From: skodak Date: Sun, 1 Jun 2008 14:25:05 +0000 (+0000) Subject: MDL-14679 towards /calendar conversion X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=62d11d771e2027d0d597478e97934ee844a4188d;p=moodle.git MDL-14679 towards /calendar conversion --- diff --git a/calendar/event.php b/calendar/event.php index 3cf31e287e..26751d7b99 100644 --- a/calendar/event.php +++ b/calendar/event.php @@ -87,7 +87,7 @@ } // 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) { @@ -105,7 +105,7 @@ 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'); } @@ -116,17 +116,17 @@ 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,''), PARAM_CLEAN); @@ -157,25 +157,27 @@ $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); } @@ -192,7 +194,7 @@ 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, '')); @@ -218,16 +220,16 @@ $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++) { @@ -240,10 +242,10 @@ $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 @@ -264,7 +266,7 @@ 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; } @@ -281,20 +283,19 @@ echo ''; echo '
'; - 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); } } @@ -308,11 +309,9 @@ $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; } @@ -358,16 +357,15 @@ 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; } @@ -446,7 +444,7 @@ 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'; } @@ -503,7 +501,7 @@ 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'); } @@ -565,6 +563,7 @@ function validate_form(&$form, &$err) { + global $DB; $form->name = trim($form->name); $form->description = trim($form->description); @@ -591,7 +590,7 @@ function validate_form(&$form, &$err) { } 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'); } @@ -602,7 +601,7 @@ function validate_form(&$form, &$err) { } 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') { @@ -623,7 +622,7 @@ function calendar_add_event_allowed($event) { // 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)) diff --git a/calendar/event_new.html b/calendar/event_new.html index 15035d5e04..2e094f3820 100644 --- a/calendar/event_new.html +++ b/calendar/event_new.html @@ -1,6 +1,6 @@ courseid) ? $form->courseid : $site->id); + $course = $DB->get_record('course', array('id'=>($form->courseid ? $form->courseid : $site->id))); ?>
diff --git a/calendar/export.php b/calendar/export.php index 41d59c2e38..1dccb7bdb8 100644 --- a/calendar/export.php +++ b/calendar/export.php @@ -10,7 +10,7 @@ $day = optional_param('cal_d', 0, PARAM_INT); $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(); @@ -32,7 +32,7 @@ if ($course->id != SITEID) { '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'); @@ -112,13 +112,13 @@ list($nextmon, $nextyr) = calendar_add_month($mon, $yr); $getvars = 'cal_d='.$day.'&cal_m='.$mon.'&cal_y='.$yr; // For filtering echo '
'; -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 '
'; -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 '
'; -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 '
'; diff --git a/calendar/lib.php b/calendar/lib.php index b80c51ab9f..30acc32090 100644 --- a/calendar/lib.php +++ b/calendar/lib.php @@ -46,6 +46,8 @@ define('CALENDAR_DEFAULT_STARTING_WEEKDAY', 1); // 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'); @@ -356,14 +358,14 @@ function calendar_get_popup($is_today, $event_timestart, $popupcontent='') { } 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 @@ -430,7 +432,7 @@ function calendar_get_upcoming($courses, $groups, $users, $daysinfuture, $maxeve 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; } @@ -586,6 +588,8 @@ function calendar_print_event($event) { * @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)) { @@ -679,7 +683,7 @@ function calendar_get_events($tstart, $tend, $users, $groups, $courses, $withdur $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(); } @@ -1125,12 +1129,13 @@ function calendar_get_module_cached(&$coursecache, $modulename, $instance) { } 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]; @@ -1202,7 +1207,7 @@ function calendar_set_referring_course($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 @@ -1219,12 +1224,12 @@ function calendar_set_filters(&$courses, &$group, &$user, $courseeventsfrom = NU // 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)); } } } @@ -1316,9 +1321,9 @@ function calendar_set_filters(&$courses, &$group, &$user, $courseeventsfrom = NU 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; } @@ -1339,10 +1344,10 @@ function calendar_set_filters(&$courses, &$group, &$user, $courseeventsfrom = NU 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; } @@ -1364,8 +1369,7 @@ function calendar_set_filters(&$courses, &$group, &$user, $courseeventsfrom = NU } function calendar_edit_event_allowed($event) { - - global $USER; + global $USER, $DB; // Must be logged in if (!isloggedin()) { @@ -1388,7 +1392,7 @@ function calendar_edit_event_allowed($event) { // 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)) @@ -1405,7 +1409,7 @@ function calendar_edit_event_allowed($event) { } 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); @@ -1418,7 +1422,7 @@ function calendar_get_default_courses($ignoreref = false) { $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; } } @@ -1572,7 +1576,7 @@ function calendar_set_filters_status($packed_bitfield) { } 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 @@ -1580,7 +1584,7 @@ function calendar_get_allowed_types(&$allowed) { $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)) { diff --git a/calendar/preferences.php b/calendar/preferences.php index 98fd0912bb..28989e413a 100644 --- a/calendar/preferences.php +++ b/calendar/preferences.php @@ -5,7 +5,7 @@ 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(); } } @@ -18,7 +18,7 @@ /// 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': diff --git a/calendar/set.php b/calendar/set.php index 0893605a1f..6d1844f34f 100644 --- a/calendar/set.php +++ b/calendar/set.php @@ -76,7 +76,7 @@ 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); diff --git a/calendar/view.php b/calendar/view.php index 9ecc0b2e3b..827e63cea2 100644 --- a/calendar/view.php +++ b/calendar/view.php @@ -98,7 +98,7 @@ // 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); @@ -125,10 +125,10 @@ // 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 } } @@ -307,7 +307,7 @@ function calendar_show_month_detailed($m, $y, $courses, $groups, $users, $course $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;