Merge branch 'wip-MDL-58688-master-c' of git://github.com/marinaglancy/moodle

This commit is contained in:
Dan Poltawski 2017-05-08 10:31:37 +01:00
commit 3cc5abc163
19 changed files with 226 additions and 375 deletions

View file

@ -252,7 +252,7 @@ class event_vault implements event_vault_interface {
true,
true,
function ($event) use ($course) {
return $event instanceof action_event_interface && $event->get_course()->get_id() == $course->id;
return $event instanceof action_event_interface && $event->get_course()->get('id') == $course->id;
}
)
);
@ -348,11 +348,6 @@ class event_vault implements event_vault_interface {
* @return event_interface|null
*/
protected function transform_from_database_record(\stdClass $record) {
if ($record->courseid == 0 && $record->instance && $record->modulename) {
list($course, $cm) = get_course_and_cm_from_instance($record->instance, $record->modulename);
$record->courseid = $course->id;
}
return $this->factory->create_instance($record);
}

View file

@ -24,6 +24,8 @@
namespace core_calendar\local\event\entities;
use core_calendar\local\event\proxies\proxy_interface;
defined('MOODLE_INTERNAL') || die();
/**

View file

@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die();
use core_calendar\local\event\entities\event;
use core_calendar\local\event\entities\repeat_event_collection;
use core_calendar\local\event\exceptions\invalid_callback_exception;
use core_calendar\local\event\proxies\module_std_proxy;
use core_calendar\local\event\proxies\cm_info_proxy;
use core_calendar\local\event\proxies\std_proxy;
use core_calendar\local\event\value_objects\event_description;
use core_calendar\local\event\value_objects\event_times;
@ -107,6 +107,12 @@ abstract class event_abstract_factory implements event_factory_interface {
}
public function create_instance(\stdClass $dbrow) {
if ($dbrow->modulename && $dbrow->instance && $dbrow->courseid == 0) {
// Some events (for example user overrides) may contain module instance but not course id. Find course id.
$cm = calendar_get_module_cached($this->modulecachereference, $dbrow->modulename, $dbrow->instance);
$dbrow->courseid = $cm->course;
}
$bailcheck = $this->bailoutcheck;
$bail = $bailcheck($dbrow);
@ -126,9 +132,8 @@ abstract class event_abstract_factory implements event_factory_interface {
$module = null;
$subscription = null;
if ($dbrow->courseid == 0 && !empty($dbrow->modulename)) {
$cm = get_coursemodule_from_instance($dbrow->modulename, $dbrow->instance);
$dbrow->courseid = get_course($cm->course)->id;
if ($dbrow->modulename && $dbrow->instance) {
$module = new cm_info_proxy($dbrow->modulename, $dbrow->instance, $dbrow->courseid);
}
$course = new std_proxy($dbrow->courseid, function($id) {
@ -148,20 +153,6 @@ abstract class event_abstract_factory implements event_factory_interface {
});
}
if ($dbrow->instance && !empty($dbrow->modulename)) {
$module = new module_std_proxy(
$dbrow->modulename,
$dbrow->instance,
function($modulename, $instance) {
return calendar_get_module_cached(
$this->modulecachereference,
$modulename,
$instance
);
}
);
}
if ($dbrow->subscriptionid) {
$subscription = new std_proxy($dbrow->subscriptionid, function($id) {
return calendar_get_subscription($id);

View file

@ -103,9 +103,9 @@ class event_mapper implements event_mapper_interface {
'name' => $event->get_name(),
'description' => $event->get_description()->get_value(),
'format' => $event->get_description()->get_format(),
'courseid' => $event->get_course() ? $event->get_course()->get_id() : null,
'groupid' => $event->get_group() ? $event->get_group()->get_id() : null,
'userid' => $event->get_user() ? $event->get_user()->get_id() : null,
'courseid' => $event->get_course() ? $event->get_course()->get('id') : null,
'groupid' => $event->get_group() ? $event->get_group()->get('id') : null,
'userid' => $event->get_user() ? $event->get_user()->get('id') : null,
'repeatid' => $event->get_repeats()->get_id(),
'modulename' => $event->get_course_module() ? $event->get_course_module()->get('modname') : null,
'instance' => $event->get_course_module() ? $event->get_course_module()->get('instance') : null,
@ -115,7 +115,7 @@ class event_mapper implements event_mapper_interface {
'timesort' => $event->get_times()->get_sort_time()->getTimestamp(),
'visible' => $event->is_visible() ? 1 : 0,
'timemodified' => $event->get_times()->get_modified_time()->getTimestamp(),
'subscriptionid' => $event->get_subscription() ? $event->get_subscription()->get_id() : null,
'subscriptionid' => $event->get_subscription() ? $event->get_subscription()->get('id') : null,
'actionname' => $action ? $action->get_name() : null,
'actionurl' => $action ? $action->get_url() : null,
'actionnum' => $action ? $action->get_item_count() : null,

View file

@ -15,7 +15,7 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Course module stdClass proxy.
* Course module cm_info proxy.
*
* @package core_calendar
* @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
@ -38,27 +38,46 @@ defined('MOODLE_INTERNAL') || die();
* @copyright 2017 Cameron Ball <cameron@cameron1729.xyz>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class module_std_proxy extends std_proxy implements proxy_interface {
class cm_info_proxy implements proxy_interface {
/** @var \stdClass */
protected $base;
/** @var \cm_info */
protected $cm;
/**
* module_std_proxy constructor.
* cm_info_proxy constructor.
*
* @param int $modulename The module name.
* @param callable $instance The module instance.
* @param callable $callback Callback to load the class.
* @param \stdClass $base Class containing base values.
* @param string $modname The module name.
* @param int $instance The module instance.
* @param int $courseid course id this module belongs to
*/
public function __construct($modulename, $instance, callable $callback, \stdClass $base = null) {
$this->modulename = $modulename;
$this->instance = $instance;
$this->callbackargs = [$modulename, $instance];
$this->callback = $callback;
$this->base = $base = is_null($base) ? new \stdClass() : $base;
$this->base->modulename = $modulename;
$this->base->instance = $instance;
public function __construct($modname, $instance, $courseid) {
$this->base = (object)['course' => $courseid, 'modname' => $modname, 'instance' => $instance];
}
public function get_id() {
return $this->get_proxied_instance()->id;
/**
* Retrieve a member of the proxied class.
*
* @param string $member The name of the member to retrieve
* @return mixed The member.
*/
public function get($member) {
if ($this->base && property_exists($this->base, $member)) {
return $this->base->{$member};
}
return $this->get_proxied_instance()->{$member};
}
/**
* Get the full instance of the proxied class.
*
* @return \stdClass
*/
public function get_proxied_instance() {
if (!$this->cm) {
$this->cm = get_fast_modinfo($this->base->course)->instances[$this->base->modname][$this->base->instance];
}
return $this->cm;
}
}

View file

@ -43,23 +43,6 @@ interface proxy_interface {
*/
public function get($member);
/**
* Retrieve the ID of the proxied class.
* @return int The proxied class' ID.
*/
public function get_id();
/**
* Set a member of the proxied class.
*
* @param string $member The name of the member to set
* @param mixed $value The value to set the member to
* @throws \core_calendar\local\event\exceptions\member_does_not_exist_exception If the proxied class does not have the
* requested member.
* @return void
*/
public function set($member, $value);
/**
* Get the full instance of the proxied class.
*

View file

@ -78,13 +78,9 @@ class std_proxy implements proxy_interface {
$this->base = $base;
}
public function get_id() {
return $this->id;
}
public function get($member) {
if ($member === 'id') {
return $this->get_id();
return $this->id;
}
if ($this->base && property_exists($this->base, $member)) {
@ -98,14 +94,6 @@ class std_proxy implements proxy_interface {
return $this->get_proxied_instance()->{$member};
}
public function set($member, $value) {
if (!property_exists($this->get_proxied_instance(), $member)) {
throw new member_does_not_exist_exception(sprintf('Member %s does not exist', $member));
}
$this->get_proxied_instance()->{$member} = $value;
}
public function get_proxied_instance() {
$callback = $this->callback;
return $this->class = $this->class ? $this->class : $callback(...$this->callbackargs);