Skip to content

Commit

Permalink
Merge branch 'MDL-37101-master' of git://github.com/ankitagarwal/moodle
Browse files Browse the repository at this point in the history
Conflicts:
	calendar/tests/externallib_tests.php
  • Loading branch information
stronk7 committed Jan 29, 2013
2 parents 6155915 + 15c0b1c commit 0ed03cc
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 0 deletions.
126 changes: 126 additions & 0 deletions calendar/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,130 @@ public static function get_calendar_events_returns() {
)
);
}

/**
* Returns description of method parameters.
*
* @return external_function_parameters.
* @since Moodle 2.5
*/
public static function create_calendar_events_parameters() {
// Userid is always current user, so no need to get it from client.
// Module based calendar events are not allowed here. Hence no need of instance and modulename.
// subscription id and uuid is not allowed as this is not an ical api.
return new external_function_parameters(
array('events' => new external_multiple_structure(
new external_single_structure(
array(
'name' => new external_value(PARAM_TEXT, 'event name', VALUE_REQUIRED, '', NULL_NOT_ALLOWED),
'description' => new external_value(PARAM_RAW, 'Description', VALUE_DEFAULT, null, NULL_ALLOWED),
'format' => new external_format_value('description', VALUE_DEFAULT),
'courseid' => new external_value(PARAM_INT, 'course id', VALUE_DEFAULT, 0, NULL_NOT_ALLOWED),
'groupid' => new external_value(PARAM_INT, 'group id', VALUE_DEFAULT, 0, NULL_NOT_ALLOWED),
'repeats' => new external_value(PARAM_INT, 'number of repeats', VALUE_DEFAULT, 0, NULL_NOT_ALLOWED),
'eventtype' => new external_value(PARAM_TEXT, 'Event type', VALUE_DEFAULT, 'user', NULL_NOT_ALLOWED),
'timestart' => new external_value(PARAM_INT, 'timestart', VALUE_DEFAULT, time(), NULL_NOT_ALLOWED),
'timeduration' => new external_value(PARAM_INT, 'time duration', VALUE_DEFAULT, 0, NULL_NOT_ALLOWED),
'visible' => new external_value(PARAM_INT, 'visible', VALUE_DEFAULT, 1, NULL_NOT_ALLOWED),
'sequence' => new external_value(PARAM_INT, 'sequence', VALUE_DEFAULT, 1, NULL_NOT_ALLOWED),
), 'event')
)
)
);
}

/**
* Delete Calendar events.
*
* @param array $events A list of events to create.
* @return array array of events created.
* @since Moodle 2.5
* @throws moodle_exception if user doesnt have the permission to create events.
*/
public static function create_calendar_events($events) {
global $CFG, $DB, $USER;
require_once($CFG->dirroot."/calendar/lib.php");

// Parameter validation.
$params = self::validate_parameters(self::create_calendar_events_parameters(), array('events' => $events));

$transaction = $DB->start_delegated_transaction();
$return = array();
$warnings = array();

foreach ($params['events'] as $event) {

// Let us set some defaults.
$event['userid'] = $USER->id;
$event['modulename'] = '';
$event['instance'] = 0;
$event['subscriptionid'] = null;
$event['uuid']= '';
$event['format'] = external_validate_format($event['format']);
if ($event['repeats'] > 0) {
$event['repeat'] = 1;
} else {
$event['repeat'] = 0;
}

$eventobj = new calendar_event($event);

// Let's check if the user is allowed to delete an event.
if (!calendar_add_event_allowed($eventobj)) {
$warnings [] = array('item' => $event['name'], 'warningcode' => 'nopermissions', 'message' => 'you donot have permissions to create this event');
continue;
}
// Let's create the event.
$var = $eventobj->create($event);
$var = (array)$var->properties();
if ($event['repeat']) {
$children = $DB->get_records('event', array('repeatid' => $var['id']));
foreach ($children as $child) {
$return[] = (array) $child;
}
} else {
$return[] = $var;
}
}

// Everything done smoothly, let's commit.
$transaction->allow_commit();
return array('events' => $return, 'warnings' => $warnings);
}

/**
* Returns description of method result value.
*
* @return external_description.
* @since Moodle 2.5
*/
public static function create_calendar_events_returns() {
return new external_single_structure(
array(
'events' => new external_multiple_structure( new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'event id'),
'name' => new external_value(PARAM_TEXT, 'event name'),
'description' => new external_value(PARAM_RAW, 'Description', VALUE_OPTIONAL),
'format' => new external_format_value('description'),
'courseid' => new external_value(PARAM_INT, 'course id'),
'groupid' => new external_value(PARAM_INT, 'group id'),
'userid' => new external_value(PARAM_INT, 'user id'),
'repeatid' => new external_value(PARAM_INT, 'repeat id', VALUE_OPTIONAL),
'modulename' => new external_value(PARAM_TEXT, 'module name', VALUE_OPTIONAL),
'instance' => new external_value(PARAM_INT, 'instance id'),
'eventtype' => new external_value(PARAM_TEXT, 'Event type'),
'timestart' => new external_value(PARAM_INT, 'timestart'),
'timeduration' => new external_value(PARAM_INT, 'time duration'),
'visible' => new external_value(PARAM_INT, 'visible'),
'uuid' => new external_value(PARAM_TEXT, 'unique id of ical events', VALUE_OPTIONAL, '', NULL_NOT_ALLOWED),
'sequence' => new external_value(PARAM_INT, 'sequence'),
'timemodified' => new external_value(PARAM_INT, 'time modified'),
'subscriptionid' => new external_value(PARAM_INT, 'Subscription id', VALUE_OPTIONAL),
), 'event')
),
'warnings' => new external_warnings()
)
);
}
}
78 changes: 78 additions & 0 deletions calendar/tests/externallib_tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,4 +360,82 @@ public function test_get_calendar_events() {
$this->assertEquals(1, count($events['events'])); // site.
$this->assertEquals(0, count($events['warnings']));
}

/**
* Test core_calendar_external::create_calendar_events
*/
public function test_core_create_calendar_events() {
global $DB, $USER, $SITE;

$this->resetAfterTest(true);
$this->setAdminUser();

// Create a few stuff to test with.
$user = $this->getDataGenerator()->create_user();
$course = $this->getDataGenerator()->create_course();
$record = new stdClass();
$record->courseid = $course->id;
$group = $this->getDataGenerator()->create_group($record);

$prevcount = $DB->count_records('event');

// Let's create a few events.
$events = array (
array('name' => 'site', 'courseid' => $SITE->id, 'eventtype' => 'site'),
array('name' => 'course', 'courseid' => $course->id, 'eventtype' => 'course', 'repeats' => 2),
array('name' => 'group', 'courseid' => $course->id, 'groupid' => $group->id, 'eventtype' => 'group'),
array('name' => 'user')
);
$eventsret = core_calendar_external::create_calendar_events($events);

// Check to see if things were created properly.
$aftercount = $DB->count_records('event');
$this->assertEquals($prevcount + 5, $aftercount);
$this->assertEquals(5, count($eventsret['events']));
$this->assertEquals(0, count($eventsret['warnings']));

$sitecontext = context_system::instance();
$coursecontext = context_course::instance($course->id);

$this->setUser($user);
$prevcount = $aftercount;
$events = array (
array('name' => 'course', 'courseid' => $course->id, 'eventtype' => 'course', 'repeats' => 2),
array('name' => 'group', 'courseid' => $course->id, 'groupid' => $group->id, 'eventtype' => 'group'),
array('name' => 'user')
);
$role = $DB->get_record('role', array('shortname' => 'student'));
$this->getDataGenerator()->enrol_user($user->id, $course->id, $role->id);
groups_add_member($group, $user);
$this->assignUserCapability('moodle/calendar:manageentries', $coursecontext->id, $role->id);
$this->assignUserCapability('moodle/calendar:managegroupentries', $coursecontext->id, $role->id);
$eventsret = core_calendar_external::create_calendar_events($events);
$eventsret = external_api::clean_returnvalue(core_calendar_external::create_calendar_events_returns(), $eventsret);
// Check to see if things were created properly.
$aftercount = $DB->count_records('event');
$this->assertEquals($prevcount + 4, $aftercount);
$this->assertEquals(4, count($eventsret['events']));
$this->assertEquals(0, count($eventsret['warnings']));

// Check to see nothing was created without proper permission.
$this->setGuestUser();
$prevcount = $DB->count_records('event');
$eventsret = core_calendar_external::create_calendar_events($events);
$eventsret = external_api::clean_returnvalue(core_calendar_external::create_calendar_events_returns(), $eventsret);
$aftercount = $DB->count_records('event');
$this->assertEquals($prevcount, $aftercount);
$this->assertEquals(0, count($eventsret['events']));
$this->assertEquals(3, count($eventsret['warnings']));

$this->setUser($user);
$this->unassignUserCapability('moodle/calendar:manageentries', $coursecontext->id, $role->id);
$this->unassignUserCapability('moodle/calendar:managegroupentries', $coursecontext->id, $role->id);
$prevcount = $DB->count_records('event');
$eventsret = core_calendar_external::create_calendar_events($events);
$eventsret = external_api::clean_returnvalue(core_calendar_external::create_calendar_events_returns(), $eventsret);
$aftercount = $DB->count_records('event');
$this->assertEquals($prevcount + 1, $aftercount); // User event.
$this->assertEquals(1, count($eventsret['events']));
$this->assertEquals(2, count($eventsret['warnings']));
}
}
1 change: 1 addition & 0 deletions lang/en/calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

$string['advancedoptions'] = 'Advanced options';
$string['allday'] = 'All day';
$string['addevent'] = 'Add events';
$string['annually'] = 'Annually';
$string['calendar'] = 'Calendar';
$string['calendarheading'] = '{$a} Calendar';
Expand Down
8 changes: 8 additions & 0 deletions lib/db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,14 @@
'capabilities'=> 'moodle/calendar:manageentries', 'moodle/calendar:manageownentries', 'moodle/calendar:managegroupentries'
),

'core_calendar_create_calendar_events' => array(
'classname' => 'core_calendar_external',
'methodname' => 'create_calendar_events',
'description' => 'Create calendar events',
'classpath' => 'calendar/externallib.php',
'type' => 'write',
'capabilities'=> 'moodle/calendar:manageentries', 'moodle/calendar:manageownentries', 'moodle/calendar:managegroupentries'
),
);

$services = array(
Expand Down

0 comments on commit 0ed03cc

Please sign in to comment.