Date post: | 16-Apr-2017 |
Category: |
Software |
Upload: | dan-poltawski |
View: | 188 times |
Download: | 1 times |
API Changes in Moodle 3.3
Dan PoltawskiIntegrator, Moodle HQ
@dan_p #mootieuk17
Copyright 2017 © Moodle Pty Ltd - CC SA - [email protected]
Persistent• https://
docs.moodle.org/dev/Persistent
• Model/Active Record for Moodle
<?phpclass status extends \core\persistent { /** Table name for the persistent. */ const TABLE = 'status'; /** * Return the definition of the properties of * * @return array */ protected static function define_properties() { return array( 'userid' => array( 'type' => PARAM_INT, ), 'message' => array( 'type' => PARAM_RAW, ), ); }}
Copyright 2017 © Moodle Pty Ltd - CC SA - [email protected]
// Fetches an object from database based on its ID.$id = 123;$persistent = new status($id); // Create object in the database.$data = new stdClass();$data->message = 'Hello new world';$persistent = new persistent(0, $data);$persistent->create();// $persistent->get('id') will now return an id. // Load an object from the database, and update it.$id = 123;$persistent = new status($id);$persistent->set('message', 'Hello new world!');$persistent->update(); // Reset the instance to the values in the database.$persistent->read(); // Permanently delete the object from the database.$persistent->delete();
Persistent
Copyright 2017 © Moodle Pty Ltd - CC SA - [email protected]
• Custom getter and setters• get/set_propertyname()
public function get_author() { return core_user::get_user($this->get('userid'));}
• Validation• Basic PARAM_* validation by default• Additional validation protected function validate_userid($value) { if (!core_user::is_real_user($value, true)) { return new lang_string('invaliduserid', 'error'); } return true;}
Persistent
Copyright 2017 © Moodle Pty Ltd - CC SA - [email protected]
Persistent - Fetching// Use the constructor to fetch one object from its ID.$persistent = new status($id); // Get one record from a set of conditions.$persistent = status::get_record(['userid' => $userid, 'message' => 'Hello world!']); // Get multiple records from a set of conditions.$persistents = status::get_records(['userid' => $userid]); // Count the records.$count = status::count_records(['userid' => $userid]); // Check whether a record exists.$exists = status::record_exists($id);
Copyright 2017 © Moodle Pty Ltd - CC SA - [email protected]
Persistent Forms• https://docs.moodle.org/dev/Persistent_formclass status_form extends \core\form\persistent { /** @var string Persistent class name. */ protected static $persistentclass = 'example\\status';
public function definition() { $mform = $this->_form; $mform->addElement('hidden', 'userid'); $mform->setConstant('userid', $this->_customdata['userid']); $mform->addElement('editor', 'message', 'Message'); $mform->addElement('text', 'location', 'Location'); $this->add_action_buttons(); }}
Copyright 2017 © Moodle Pty Ltd - CC SA - [email protected]
Exporters• https://docs.moodle.org/dev/Exporter• Serialise an object to stdClass• Simplify external functions and
templatable::export_for_template()
public static function get_users_returns() { return external_multiple_structure( user_exporter::get_read_structure() );}// e.g.
foreach ($users as $userdata) { $exporter = new user_exporter($userdata); $result[] = $exporter->export($output); }
Copyright 2017 © Moodle Pty Ltd - CC SA - [email protected]
Persistent and Exporterclass status_exporter extends \core\external\persistent_exporter { /** * Returns the specific class the persistent should be an instance of. * * @return string */ protected static function define_class() { return \some\namespace\status::class; }}
Copyright 2017 © Moodle Pty Ltd - CC SA - [email protected]
Calendar: Action Events
https://docs.moodle.org/dev/Calendar_API#Action_events
Callbacks: mod_xyz_core_calendar_is_event_visible(calendar_event $event)
mod_xyz_core_calendar_core_calendar_provide_event_action(calendar_event $event, \core_calendar\action_factory $factory)
mod_xyz_core_calendar_event_action_shows_item_count(calendar_event $event, $itemcount = 0)
Copyright 2017 © Moodle Pty Ltd - CC SA - [email protected]
New Hooks into page rendering
[component]_add_htmlattributes [component]_before_http_headers [component]_before_standard_html_head [component]_before_standard_top_of_body_html [component]_before_footer
Copyright 2017 © Moodle Pty Ltd - CC SA - [email protected]
Slow March away from YUI• Some YUI modules previously loaded on every page
(mcore rollup)• Grep especially for:•M.core.dialogue
• Various legacy global (javascript-static) JS functions deprecated
• Test, test, test particularly on boost
Copyright 2017 © Moodle Pty Ltd - CC SA - [email protected]
Font Awesomehttps://docs.moodle.org/dev/Moodle_icons#Font_awesome_icons
• Benefits:• 430+ icons: http://fontawesome.io/icons/ • CSS colouring of icons • Fewer HTTP requests• Good scaling
Copyright 2017 © Moodle Pty Ltd - CC SA - [email protected]
Font Awesome - Deprecations
• Required deprecation of $OUTPUT->pix_url()• distinction between icons (can be font) and ‘small images’
• Replacements:• $OUTPUT->pix_icon() / {{#pix}}…{{/pix}}• $OUTPUT->image_icon() / $OUTPUT->image_url()
Copyright 2017 © Moodle Pty Ltd - CC SA - [email protected]
Font Awesome• Enable support in theme:
$THEME->iconsystem = \core\output\icon_system::FONTAWESOME;
• Callback to define mapping in your plugin:/** * Get icon mapping for font-awesome. */ function mod_forum_get_fontawesome_icon_map() { return [ 'mod_forum:i/pinned' => 'fa-map-pin', 'mod_forum:t/selected' => 'fa-check', 'mod_forum:t/subscribed' => 'fa-envelope-o', 'mod_forum:t/unsubscribed' => 'fa-envelope-open-o', ]; }
Copyright 2017 © Moodle Pty Ltd - CC SA - [email protected]
Stealth Activities
• Course formats supporting ‘stealth’ mode:• format_base::allow_stealth_module_visibility() • $cm->is_visible_on_course_page()
Copyright 2017 © Moodle Pty Ltd - CC SA - [email protected]
File Conversion API
https://docs.moodle.org/dev/File_Converters
• Can replace unconv• Async API
$converter = new \core_files\converter();$conversion = $converter->start_conversion($file, 'pdf');$converter->poll_conversion($conversion);
Copyright 2017 © Moodle Pty Ltd - CC SA - [email protected]
File System Class
https://docs.moodle.org/dev/File_System_API
$CFG->alternative_file_system_class
Allows replacement for filepool storage (e.g. with S3-like storage systems).
Advanced: No migration/upgrade consideration by core.
Copyright 2017 © Moodle Pty Ltd - CC SA - [email protected]
OAuth 2.0 API
https://docs.moodle.org/dev/OAuth_2_API
// Get an issuer from the id.$issuer = \core\oauth2\api::get_issuer($issuerid);// Get an OAuth client from the issuer.$client = \core\oauth2\api::get_user_oauth_client($this->issuer, $returnurl, $scopes);
Copyright 2017 © Moodle Pty Ltd - CC SA - [email protected]
New mustache helpers
{{#userdate}} {{timesort}}, {{format}} {{/userdate}}
{{#shortentext}}50, Some test to shorten{{/shortentext}}
Copyright 2017 © Moodle Pty Ltd - CC SA - [email protected]
Misc
• Cache-Control: immutable used for CSS/JS • Auth plugins finally get real admin settings
• config.html -> settings.php • Continued expansion of webservice capabilities
• 🍺 to Juan
[email protected]@dan_p