Merge branch 'MDL-41149-master' of git://github.com/damyon/moodle
authorDan Poltawski <dan@moodle.com>
Mon, 2 Sep 2013 09:25:27 +0000 (17:25 +0800)
committerDan Poltawski <dan@moodle.com>
Mon, 2 Sep 2013 09:25:27 +0000 (17:25 +0800)
412 files changed:
admin/index.php
admin/plugins.php
admin/renderer.php
admin/tool/generator/classes/backend.php
admin/tool/generator/classes/course_backend.php [new file with mode: 0644]
admin/tool/generator/classes/make_form.php
admin/tool/generator/classes/site_backend.php [new file with mode: 0644]
admin/tool/generator/cli/maketestcourse.php
admin/tool/generator/cli/maketestsite.php [new file with mode: 0644]
admin/tool/generator/lang/en/tool_generator.php
admin/tool/generator/maketestcourse.php
admin/tool/generator/tests/maketestcourse_test.php
admin/tool/generator/version.php
admin/tool/uploaduser/index.php
admin/user.php
auth/db/auth.php
auth/email/auth.php
auth/ldap/auth.php
auth/ldap/ntlmsso_magic.php
auth/mnet/auth.php
backup/moodle2/restore_stepslib.php
backup/util/loggers/database_logger.class.php
backup/util/structure/tests/baseoptiogroup_test.php
backup/util/ui/renderer.php
backup/util/ui/tests/behat/restore_moodle2_courses.feature
badges/classes/observer.php
badges/external.php
badges/mybackpack.php
badges/renderer.php
badges/tests/badgeslib_test.php
blog/locallib.php
cache/classes/loaders.php
cache/stores/file/lib.php
cache/stores/memcache/lib.php
cache/stores/memcached/lib.php
cache/stores/session/lib.php
cache/stores/session/tests/session_test.php
cache/tests/cache_test.php
cache/tests/fixtures/lib.php
course/format/scorm/format.php [deleted file]
course/format/scorm/lib.php [deleted file]
course/lib.php
course/manage.php
course/rest.php
course/tests/courselib_test.php
course/view.php
enrol/ldap/lang/en/enrol_ldap.php
enrol/ldap/lib.php
enrol/manual/locallib.php
enrol/meta/classes/observer.php [new file with mode: 0644]
enrol/meta/db/events.php
enrol/meta/locallib.php
enrol/meta/tests/plugin_test.php
enrol/tests/enrollib_test.php
files/renderer.php
files/tests/externallib_test.php
grade/export/lib.php
grade/report/grader/lib.php
group/index.php
group/lib.php
group/tests/lib_test.php [new file with mode: 0644]
lang/en/backup.php
lang/en/badges.php
lang/en/cache.php
lang/en/enrol.php
lang/en/group.php
lang/en/moodle.php
lang/en/tag.php
lib/adminlib.php
lib/ajax/ajaxlib.php
lib/ajax/tests/ajaxlib_test.php [deleted file]
lib/badgeslib.php
lib/behat/classes/util.php
lib/blocklib.php
lib/classes/event/content_viewed.php [new file with mode: 0644]
lib/classes/event/group_created.php [new file with mode: 0644]
lib/classes/event/group_deleted.php [new file with mode: 0644]
lib/classes/event/group_member_added.php [new file with mode: 0644]
lib/classes/event/group_member_removed.php [new file with mode: 0644]
lib/classes/event/group_updated.php [new file with mode: 0644]
lib/classes/event/grouping_created.php [new file with mode: 0644]
lib/classes/event/grouping_deleted.php [new file with mode: 0644]
lib/classes/event/grouping_updated.php [new file with mode: 0644]
lib/classes/event/user_created.php [new file with mode: 0644]
lib/classes/event/user_deleted.php [new file with mode: 0644]
lib/classes/event/user_enrolment_created.php [new file with mode: 0644]
lib/classes/event/user_enrolment_deleted.php [new file with mode: 0644]
lib/classes/event/user_enrolment_updated.php [new file with mode: 0644]
lib/classes/event/user_loggedin.php
lib/classes/event/user_loggedout.php [new file with mode: 0644]
lib/classes/event/user_updated.php [new file with mode: 0644]
lib/classes/text.php
lib/classes/useragent.php [new file with mode: 0644]
lib/configonlylib.php
lib/db/caches.php
lib/db/events.php
lib/db/install.xml
lib/db/upgrade.php
lib/ddl/tests/ddl_test.php
lib/ddl/tests/fixtures/invalid.xml
lib/ddl/tests/fixtures/xmldb_table.xml
lib/deprecatedlib.php
lib/dml/mssql_native_moodle_database.php
lib/dml/sqlsrv_native_moodle_database.php
lib/dml/tests/dml_test.php
lib/editor/atto/db/install.php [new file with mode: 0644]
lib/editor/atto/db/subplugins.php [moved from course/format/scorm/lang/en/format_scorm.php with 68% similarity]
lib/editor/atto/lang/en/editor_atto.php [new file with mode: 0644]
lib/editor/atto/lib.php [new file with mode: 0644]
lib/editor/atto/plugins/bold/lang/en/atto_bold.php [new file with mode: 0644]
lib/editor/atto/plugins/bold/lib.php [new file with mode: 0644]
lib/editor/atto/plugins/bold/pix/bold.png [new file with mode: 0644]
lib/editor/atto/plugins/bold/pix/bold.svg [new file with mode: 0644]
lib/editor/atto/plugins/bold/version.php [moved from course/format/scorm/version.php with 73% similarity]
lib/editor/atto/plugins/bold/yui/build/moodle-atto_bold-button/moodle-atto_bold-button-debug.js [new file with mode: 0644]
lib/editor/atto/plugins/bold/yui/build/moodle-atto_bold-button/moodle-atto_bold-button-min.js [new file with mode: 0644]
lib/editor/atto/plugins/bold/yui/build/moodle-atto_bold-button/moodle-atto_bold-button.js [new file with mode: 0644]
lib/editor/atto/plugins/bold/yui/src/button/build.json [new file with mode: 0644]
lib/editor/atto/plugins/bold/yui/src/button/js/button.js [new file with mode: 0644]
lib/editor/atto/plugins/bold/yui/src/button/meta/editor.json [new file with mode: 0644]
lib/editor/atto/plugins/clear/lang/en/atto_clear.php [new file with mode: 0644]
lib/editor/atto/plugins/clear/lib.php [new file with mode: 0644]
lib/editor/atto/plugins/clear/pix/clear.png [new file with mode: 0644]
lib/editor/atto/plugins/clear/pix/clear.svg [new file with mode: 0644]
lib/editor/atto/plugins/clear/version.php [new file with mode: 0644]
lib/editor/atto/plugins/clear/yui/build/moodle-atto_clear-button/moodle-atto_clear-button-debug.js [new file with mode: 0644]
lib/editor/atto/plugins/clear/yui/build/moodle-atto_clear-button/moodle-atto_clear-button-min.js [new file with mode: 0644]
lib/editor/atto/plugins/clear/yui/build/moodle-atto_clear-button/moodle-atto_clear-button.js [new file with mode: 0644]
lib/editor/atto/plugins/clear/yui/src/button/build.json [new file with mode: 0644]
lib/editor/atto/plugins/clear/yui/src/button/js/button.js [new file with mode: 0644]
lib/editor/atto/plugins/clear/yui/src/button/meta/editor.json [new file with mode: 0644]
lib/editor/atto/plugins/html/lang/en/atto_html.php [new file with mode: 0644]
lib/editor/atto/plugins/html/lib.php [new file with mode: 0644]
lib/editor/atto/plugins/html/pix/html.png [new file with mode: 0644]
lib/editor/atto/plugins/html/pix/html.svg [new file with mode: 0644]
lib/editor/atto/plugins/html/version.php [new file with mode: 0644]
lib/editor/atto/plugins/html/yui/build/moodle-atto_html-button/moodle-atto_html-button-debug.js [new file with mode: 0644]
lib/editor/atto/plugins/html/yui/build/moodle-atto_html-button/moodle-atto_html-button-min.js [new file with mode: 0644]
lib/editor/atto/plugins/html/yui/build/moodle-atto_html-button/moodle-atto_html-button.js [new file with mode: 0644]
lib/editor/atto/plugins/html/yui/src/button/build.json [new file with mode: 0644]
lib/editor/atto/plugins/html/yui/src/button/js/button.js [new file with mode: 0644]
lib/editor/atto/plugins/html/yui/src/button/meta/editor.json [new file with mode: 0644]
lib/editor/atto/plugins/image/lang/en/atto_image.php [new file with mode: 0644]
lib/editor/atto/plugins/image/lib.php [new file with mode: 0644]
lib/editor/atto/plugins/image/pix/image.png [new file with mode: 0644]
lib/editor/atto/plugins/image/pix/image.svg [new file with mode: 0644]
lib/editor/atto/plugins/image/version.php [new file with mode: 0644]
lib/editor/atto/plugins/image/yui/build/moodle-atto_image-button/moodle-atto_image-button-debug.js [new file with mode: 0644]
lib/editor/atto/plugins/image/yui/build/moodle-atto_image-button/moodle-atto_image-button-min.js [new file with mode: 0644]
lib/editor/atto/plugins/image/yui/build/moodle-atto_image-button/moodle-atto_image-button.js [new file with mode: 0644]
lib/editor/atto/plugins/image/yui/src/button/build.json [new file with mode: 0644]
lib/editor/atto/plugins/image/yui/src/button/js/button.js [new file with mode: 0644]
lib/editor/atto/plugins/image/yui/src/button/meta/editor.json [new file with mode: 0644]
lib/editor/atto/plugins/indent/lang/en/atto_indent.php [new file with mode: 0644]
lib/editor/atto/plugins/indent/lib.php [new file with mode: 0644]
lib/editor/atto/plugins/indent/pix/indent.png [new file with mode: 0644]
lib/editor/atto/plugins/indent/pix/indent.svg [new file with mode: 0644]
lib/editor/atto/plugins/indent/version.php [new file with mode: 0644]
lib/editor/atto/plugins/indent/yui/build/moodle-atto_indent-button/moodle-atto_indent-button-debug.js [new file with mode: 0644]
lib/editor/atto/plugins/indent/yui/build/moodle-atto_indent-button/moodle-atto_indent-button-min.js [new file with mode: 0644]
lib/editor/atto/plugins/indent/yui/build/moodle-atto_indent-button/moodle-atto_indent-button.js [new file with mode: 0644]
lib/editor/atto/plugins/indent/yui/src/button/build.json [new file with mode: 0644]
lib/editor/atto/plugins/indent/yui/src/button/js/button.js [new file with mode: 0644]
lib/editor/atto/plugins/indent/yui/src/button/meta/editor.json [new file with mode: 0644]
lib/editor/atto/plugins/italic/lang/en/atto_italic.php [new file with mode: 0644]
lib/editor/atto/plugins/italic/lib.php [new file with mode: 0644]
lib/editor/atto/plugins/italic/pix/italic.png [new file with mode: 0644]
lib/editor/atto/plugins/italic/pix/italic.svg [new file with mode: 0644]
lib/editor/atto/plugins/italic/version.php [new file with mode: 0644]
lib/editor/atto/plugins/italic/yui/build/moodle-atto_italic-button/moodle-atto_italic-button-debug.js [new file with mode: 0644]
lib/editor/atto/plugins/italic/yui/build/moodle-atto_italic-button/moodle-atto_italic-button-min.js [new file with mode: 0644]
lib/editor/atto/plugins/italic/yui/build/moodle-atto_italic-button/moodle-atto_italic-button.js [new file with mode: 0644]
lib/editor/atto/plugins/italic/yui/src/button/build.json [new file with mode: 0644]
lib/editor/atto/plugins/italic/yui/src/button/js/button.js [new file with mode: 0644]
lib/editor/atto/plugins/italic/yui/src/button/meta/editor.json [new file with mode: 0644]
lib/editor/atto/plugins/link/lang/en/atto_link.php [new file with mode: 0644]
lib/editor/atto/plugins/link/lib.php [new file with mode: 0644]
lib/editor/atto/plugins/link/pix/link.png [new file with mode: 0644]
lib/editor/atto/plugins/link/pix/link.svg [new file with mode: 0644]
lib/editor/atto/plugins/link/version.php [new file with mode: 0644]
lib/editor/atto/plugins/link/yui/build/moodle-atto_link-button/moodle-atto_link-button-debug.js [new file with mode: 0644]
lib/editor/atto/plugins/link/yui/build/moodle-atto_link-button/moodle-atto_link-button-min.js [new file with mode: 0644]
lib/editor/atto/plugins/link/yui/build/moodle-atto_link-button/moodle-atto_link-button.js [new file with mode: 0644]
lib/editor/atto/plugins/link/yui/src/button/build.json [new file with mode: 0644]
lib/editor/atto/plugins/link/yui/src/button/js/button.js [new file with mode: 0644]
lib/editor/atto/plugins/link/yui/src/button/meta/editor.json [new file with mode: 0644]
lib/editor/atto/plugins/media/lang/en/atto_media.php [new file with mode: 0644]
lib/editor/atto/plugins/media/lib.php [new file with mode: 0644]
lib/editor/atto/plugins/media/pix/media.png [new file with mode: 0644]
lib/editor/atto/plugins/media/pix/media.svg [new file with mode: 0644]
lib/editor/atto/plugins/media/version.php [new file with mode: 0644]
lib/editor/atto/plugins/media/yui/build/moodle-atto_media-button/moodle-atto_media-button-debug.js [new file with mode: 0644]
lib/editor/atto/plugins/media/yui/build/moodle-atto_media-button/moodle-atto_media-button-min.js [new file with mode: 0644]
lib/editor/atto/plugins/media/yui/build/moodle-atto_media-button/moodle-atto_media-button.js [new file with mode: 0644]
lib/editor/atto/plugins/media/yui/src/button/build.json [new file with mode: 0644]
lib/editor/atto/plugins/media/yui/src/button/js/button.js [new file with mode: 0644]
lib/editor/atto/plugins/media/yui/src/button/meta/editor.json [new file with mode: 0644]
lib/editor/atto/plugins/orderedlist/lang/en/atto_orderedlist.php [new file with mode: 0644]
lib/editor/atto/plugins/orderedlist/lib.php [new file with mode: 0644]
lib/editor/atto/plugins/orderedlist/pix/orderedlist.png [new file with mode: 0644]
lib/editor/atto/plugins/orderedlist/pix/orderedlist.svg [new file with mode: 0644]
lib/editor/atto/plugins/orderedlist/version.php [new file with mode: 0644]
lib/editor/atto/plugins/orderedlist/yui/build/moodle-atto_orderedlist-button/moodle-atto_orderedlist-button-debug.js [new file with mode: 0644]
lib/editor/atto/plugins/orderedlist/yui/build/moodle-atto_orderedlist-button/moodle-atto_orderedlist-button-min.js [new file with mode: 0644]
lib/editor/atto/plugins/orderedlist/yui/build/moodle-atto_orderedlist-button/moodle-atto_orderedlist-button.js [new file with mode: 0644]
lib/editor/atto/plugins/orderedlist/yui/src/button/build.json [new file with mode: 0644]
lib/editor/atto/plugins/orderedlist/yui/src/button/js/button.js [new file with mode: 0644]
lib/editor/atto/plugins/orderedlist/yui/src/button/meta/editor.json [new file with mode: 0644]
lib/editor/atto/plugins/outdent/lang/en/atto_outdent.php [new file with mode: 0644]
lib/editor/atto/plugins/outdent/lib.php [new file with mode: 0644]
lib/editor/atto/plugins/outdent/pix/outdent.png [new file with mode: 0644]
lib/editor/atto/plugins/outdent/pix/outdent.svg [new file with mode: 0644]
lib/editor/atto/plugins/outdent/version.php [new file with mode: 0644]
lib/editor/atto/plugins/outdent/yui/build/moodle-atto_outdent-button/moodle-atto_outdent-button-debug.js [new file with mode: 0644]
lib/editor/atto/plugins/outdent/yui/build/moodle-atto_outdent-button/moodle-atto_outdent-button-min.js [new file with mode: 0644]
lib/editor/atto/plugins/outdent/yui/build/moodle-atto_outdent-button/moodle-atto_outdent-button.js [new file with mode: 0644]
lib/editor/atto/plugins/outdent/yui/src/button/build.json [new file with mode: 0644]
lib/editor/atto/plugins/outdent/yui/src/button/js/button.js [new file with mode: 0644]
lib/editor/atto/plugins/outdent/yui/src/button/meta/editor.json [new file with mode: 0644]
lib/editor/atto/plugins/strike/lang/en/atto_strike.php [new file with mode: 0644]
lib/editor/atto/plugins/strike/lib.php [new file with mode: 0644]
lib/editor/atto/plugins/strike/pix/strike.png [new file with mode: 0644]
lib/editor/atto/plugins/strike/pix/strike.svg [new file with mode: 0644]
lib/editor/atto/plugins/strike/version.php [new file with mode: 0644]
lib/editor/atto/plugins/strike/yui/build/moodle-atto_strike-button/moodle-atto_strike-button-debug.js [new file with mode: 0644]
lib/editor/atto/plugins/strike/yui/build/moodle-atto_strike-button/moodle-atto_strike-button-min.js [new file with mode: 0644]
lib/editor/atto/plugins/strike/yui/build/moodle-atto_strike-button/moodle-atto_strike-button.js [new file with mode: 0644]
lib/editor/atto/plugins/strike/yui/src/button/build.json [new file with mode: 0644]
lib/editor/atto/plugins/strike/yui/src/button/js/button.js [new file with mode: 0644]
lib/editor/atto/plugins/strike/yui/src/button/meta/editor.json [new file with mode: 0644]
lib/editor/atto/plugins/title/lang/en/atto_title.php [new file with mode: 0644]
lib/editor/atto/plugins/title/lib.php [new file with mode: 0644]
lib/editor/atto/plugins/title/pix/title.png [new file with mode: 0644]
lib/editor/atto/plugins/title/pix/title.svg [new file with mode: 0644]
lib/editor/atto/plugins/title/version.php [new file with mode: 0644]
lib/editor/atto/plugins/title/yui/build/moodle-atto_title-button/moodle-atto_title-button-debug.js [new file with mode: 0644]
lib/editor/atto/plugins/title/yui/build/moodle-atto_title-button/moodle-atto_title-button-min.js [new file with mode: 0644]
lib/editor/atto/plugins/title/yui/build/moodle-atto_title-button/moodle-atto_title-button.js [new file with mode: 0644]
lib/editor/atto/plugins/title/yui/src/button/build.json [new file with mode: 0644]
lib/editor/atto/plugins/title/yui/src/button/js/button.js [new file with mode: 0644]
lib/editor/atto/plugins/title/yui/src/button/meta/editor.json [new file with mode: 0644]
lib/editor/atto/plugins/underline/lang/en/atto_underline.php [new file with mode: 0644]
lib/editor/atto/plugins/underline/lib.php [new file with mode: 0644]
lib/editor/atto/plugins/underline/pix/underline.png [new file with mode: 0644]
lib/editor/atto/plugins/underline/pix/underline.svg [new file with mode: 0644]
lib/editor/atto/plugins/underline/version.php [new file with mode: 0644]
lib/editor/atto/plugins/underline/yui/build/moodle-atto_underline-button/moodle-atto_underline-button-debug.js [new file with mode: 0644]
lib/editor/atto/plugins/underline/yui/build/moodle-atto_underline-button/moodle-atto_underline-button-min.js [new file with mode: 0644]
lib/editor/atto/plugins/underline/yui/build/moodle-atto_underline-button/moodle-atto_underline-button.js [new file with mode: 0644]
lib/editor/atto/plugins/underline/yui/src/button/build.json [new file with mode: 0644]
lib/editor/atto/plugins/underline/yui/src/button/js/button.js [new file with mode: 0644]
lib/editor/atto/plugins/underline/yui/src/button/meta/editor.json [new file with mode: 0644]
lib/editor/atto/plugins/unlink/lang/en/atto_unlink.php [new file with mode: 0644]
lib/editor/atto/plugins/unlink/lib.php [new file with mode: 0644]
lib/editor/atto/plugins/unlink/pix/unlink.png [new file with mode: 0644]
lib/editor/atto/plugins/unlink/pix/unlink.svg [new file with mode: 0644]
lib/editor/atto/plugins/unlink/version.php [new file with mode: 0644]
lib/editor/atto/plugins/unlink/yui/build/moodle-atto_unlink-button/moodle-atto_unlink-button-debug.js [new file with mode: 0644]
lib/editor/atto/plugins/unlink/yui/build/moodle-atto_unlink-button/moodle-atto_unlink-button-min.js [new file with mode: 0644]
lib/editor/atto/plugins/unlink/yui/build/moodle-atto_unlink-button/moodle-atto_unlink-button.js [new file with mode: 0644]
lib/editor/atto/plugins/unlink/yui/src/button/build.json [new file with mode: 0644]
lib/editor/atto/plugins/unlink/yui/src/button/js/button.js [new file with mode: 0644]
lib/editor/atto/plugins/unlink/yui/src/button/meta/editor.json [new file with mode: 0644]
lib/editor/atto/plugins/unorderedlist/lang/en/atto_unorderedlist.php [new file with mode: 0644]
lib/editor/atto/plugins/unorderedlist/lib.php [new file with mode: 0644]
lib/editor/atto/plugins/unorderedlist/pix/unorderedlist.png [new file with mode: 0644]
lib/editor/atto/plugins/unorderedlist/pix/unorderedlist.svg [new file with mode: 0644]
lib/editor/atto/plugins/unorderedlist/version.php [new file with mode: 0644]
lib/editor/atto/plugins/unorderedlist/yui/build/moodle-atto_unorderedlist-button/moodle-atto_unorderedlist-button-debug.js [new file with mode: 0644]
lib/editor/atto/plugins/unorderedlist/yui/build/moodle-atto_unorderedlist-button/moodle-atto_unorderedlist-button-min.js [new file with mode: 0644]
lib/editor/atto/plugins/unorderedlist/yui/build/moodle-atto_unorderedlist-button/moodle-atto_unorderedlist-button.js [new file with mode: 0644]
lib/editor/atto/plugins/unorderedlist/yui/src/button/build.json [new file with mode: 0644]
lib/editor/atto/plugins/unorderedlist/yui/src/button/js/button.js [new file with mode: 0644]
lib/editor/atto/plugins/unorderedlist/yui/src/button/meta/editor.json [new file with mode: 0644]
lib/editor/atto/styles.css [new file with mode: 0644]
lib/editor/atto/version.php [new file with mode: 0644]
lib/editor/atto/yui/build/moodle-editor_atto-editor/moodle-editor_atto-editor-debug.js [new file with mode: 0644]
lib/editor/atto/yui/build/moodle-editor_atto-editor/moodle-editor_atto-editor-min.js [new file with mode: 0644]
lib/editor/atto/yui/build/moodle-editor_atto-editor/moodle-editor_atto-editor.js [new file with mode: 0644]
lib/editor/atto/yui/src/editor/build.json [new file with mode: 0644]
lib/editor/atto/yui/src/editor/js/editor.js [new file with mode: 0644]
lib/editor/atto/yui/src/editor/meta/editor.json [new file with mode: 0644]
lib/editor/tinymce/lib.php
lib/editor/tinymce/module.js
lib/editor/tinymce/plugins/spellchecker/lib.php
lib/editor/tinymce/version.php
lib/editorlib.php
lib/enrollib.php
lib/excellib.class.php
lib/external/tests/external_test.php
lib/filelib.php
lib/filestorage/tests/file_storage_test.php
lib/filestorage/tests/zip_packer_test.php
lib/form/filemanager.js
lib/form/tests/dateselector_test.php
lib/form/tests/datetimeselector_test.php
lib/form/tests/duration_test.php
lib/grade/tests/fixtures/lib.php
lib/grade/tests/grade_category_test.php
lib/grade/tests/grade_grade_test.php
lib/grade/tests/grade_item_test.php
lib/grade/tests/grade_scale_test.php
lib/medialib.php
lib/modinfolib.php
lib/moodlelib.php
lib/navigationlib.php
lib/outputcomponents.php
lib/outputlib.php
lib/outputrenderers.php
lib/pagelib.php
lib/phpunit/tests/advanced_test.php
lib/phpunit/tests/basic_test.php
lib/pluginlib.php
lib/setuplib.php
lib/testing/tests/generator_test.php
lib/tests/configonlylib_test.php
lib/tests/event_content_viewed_test.php [new file with mode: 0644]
lib/tests/fixtures/event_fixtures.php
lib/tests/medialib_test.php
lib/tests/moodlelib_test.php
lib/tests/navigationlib_test.php
lib/tests/text_test.php
lib/tests/theme_config_test.php
lib/tests/useragent_test.php [new file with mode: 0644]
lib/upgrade.txt
lib/yui/build/moodle-core-notification-dialogue/moodle-core-notification-dialogue-debug.js
lib/yui/build/moodle-core-notification-dialogue/moodle-core-notification-dialogue-min.js
lib/yui/build/moodle-core-notification-dialogue/moodle-core-notification-dialogue.js
lib/yui/build/moodle-core-notification/moodle-core-notification-debug.js
lib/yui/src/notification/js/dialogue.js
lib/yui/src/notification/js/notification.js
mod/assign/backup/moodle2/restore_assign_stepslib.php
mod/assign/classes/event/all_submissions_downloaded.php [new file with mode: 0644]
mod/assign/classes/event/assessable_submitted.php
mod/assign/classes/event/extension_granted.php [new file with mode: 0644]
mod/assign/classes/event/identities_revealed.php [new file with mode: 0644]
mod/assign/classes/event/marker_updated.php [new file with mode: 0644]
mod/assign/classes/event/statement_accepted.php [new file with mode: 0644]
mod/assign/classes/event/submission_duplicated.php [new file with mode: 0644]
mod/assign/classes/event/submission_graded.php [new file with mode: 0644]
mod/assign/classes/event/submission_locked.php [new file with mode: 0644]
mod/assign/classes/event/submission_status_updated.php [new file with mode: 0644]
mod/assign/classes/event/submission_unlocked.php [new file with mode: 0644]
mod/assign/classes/event/submission_updated.php [new file with mode: 0644]
mod/assign/classes/event/workflow_state_updated.php [new file with mode: 0644]
mod/assign/lang/en/assign.php
mod/assign/locallib.php
mod/assign/tests/base_test.php
mod/assign/tests/locallib_test.php
mod/choice/lib.php
mod/choice/renderer.php
mod/choice/report.php
mod/choice/view.php
mod/data/view.php
mod/forum/classes/observer.php [new file with mode: 0644]
mod/forum/db/events.php
mod/forum/lib.php
mod/forum/tests/lib_test.php
mod/lesson/format.php
mod/lesson/import.php
mod/lti/locallib.php
mod/quiz/renderer.php
mod/quiz/upgrade.txt
mod/scorm/lang/en/scorm.php
mod/scorm/lib.php
mod/scorm/locallib.php
mod/scorm/mod_form.php
mod/scorm/tests/packages/badscorm.zip [new file with mode: 0644]
mod/scorm/tests/packages/invalid.zip [new file with mode: 0644]
mod/scorm/tests/packages/validaicc.zip [new file with mode: 0644]
mod/scorm/tests/packages/validscorm.zip [new file with mode: 0644]
mod/scorm/tests/validatepackage_test.php [new file with mode: 0644]
mod/wiki/pagelib.php
phpunit.xml.dist
question/behaviour/informationitem/lang/en/qbehaviour_informationitem.php
report/log/classes/event/content_viewed.php [new file with mode: 0644]
report/log/graph.php
report/log/index.php
report/log/user.php
report/loglive/classes/event/content_viewed.php [new file with mode: 0644]
report/loglive/index.php
report/outline/classes/event/content_viewed.php [new file with mode: 0644]
report/outline/index.php
report/outline/user.php
report/participation/classes/event/content_viewed.php [new file with mode: 0644]
report/participation/index.php
report/stats/classes/event/content_viewed.php [new file with mode: 0644]
report/stats/graph.php
report/stats/index.php
report/stats/user.php
repository/filepicker.js
repository/url/locallib.php
tag/manage.php
theme/base/style/core.css
theme/bootstrapbase/config.php
theme/bootstrapbase/less/moodle/bootstrapoverride.less
theme/bootstrapbase/less/moodle/core.less
theme/bootstrapbase/less/moodle/filemanager.less
theme/bootstrapbase/less/moodle/forms.less
theme/bootstrapbase/less/moodle/responsive.less
theme/bootstrapbase/less/moodle/user.less
theme/bootstrapbase/style/moodle.css
theme/index.php
theme/mymobile/config.php
theme/switchdevice.php
user/edit.php
user/editadvanced.php
user/emailupdate.php
user/filters/lib.php
user/lib.php
user/tests/userlib_test.php [new file with mode: 0644]
version.php

index 1e9e810..466832c 100644 (file)
@@ -59,6 +59,10 @@ if (empty($_GET['cache']) and empty($_POST['cache']) and empty($_GET['sesskey'])
     if (function_exists('opcache_reset')) {
         opcache_reset();
     }
+    $cache = 0;
+
+} else {
+    $cache = 1;
 }
 
 require('../config.php');
@@ -74,22 +78,28 @@ $showallplugins = optional_param('showallplugins', 0, PARAM_BOOL);
 $agreelicense   = optional_param('agreelicense', 0, PARAM_BOOL);
 $fetchupdates   = optional_param('fetchupdates', 0, PARAM_BOOL);
 $newaddonreq    = optional_param('installaddonrequest', null, PARAM_RAW);
-$cache          = optional_param('cache', 0, PARAM_BOOL);
 
 // Set up PAGE.
 $url = new moodle_url('/admin/index.php');
-if (!is_null($newaddonreq)) {
-    // We need to set the eventual add-on installation request in the $PAGE's URL
-    // so that it is stored in $SESSION->wantsurl and the admin is redirected
-    // correctly once they are logged-in.
-    $url->param('installaddonrequest', $newaddonreq);
-}
 if ($cache) {
-    $url->param('cache', $cache);
+    $url->param('cache', 1);
 }
 $PAGE->set_url($url);
 unset($url);
 
+// Are we returning from an add-on installation request at moodle.org/plugins?
+if ($newaddonreq and !$cache and empty($CFG->disableonclickaddoninstall)) {
+    $target = new moodle_url('/admin/tool/installaddon/index.php', array(
+        'installaddonrequest' => $newaddonreq,
+        'confirm' => 0));
+    if (!isloggedin() or isguestuser()) {
+        // Login and go the the add-on tool page.
+        $SESSION->wantsurl = $target->out();
+        redirect(get_login_url());
+    }
+    redirect($target);
+}
+
 $PAGE->set_pagelayout('admin'); // Set a default pagelayout
 
 $documentationlink = '<a href="http://docs.moodle.org/en/Installation">Installation docs</a>';
@@ -153,6 +163,7 @@ if (!core_tables_exist()) {
         $PAGE->set_heading($strinstallation);
         $PAGE->set_cacheable(false);
 
+        /** @var core_admin_renderer $output */
         $output = $PAGE->get_renderer('core', 'admin');
         echo $output->install_licence_page();
         die();
@@ -167,6 +178,7 @@ if (!core_tables_exist()) {
         $PAGE->set_heading($strinstallation . ' - Moodle ' . $CFG->target_release);
         $PAGE->set_cacheable(false);
 
+        /** @var core_admin_renderer $output */
         $output = $PAGE->get_renderer('core', 'admin');
         echo $output->install_environment_page($maturity, $envstatus, $environment_results, $release);
         die();
@@ -221,14 +233,12 @@ if (empty($CFG->version)) {
 }
 
 // Detect config cache inconsistency, this happens when you switch branches on dev servers.
-if ($cache) {
-    if ($CFG->version != $DB->get_field('config', 'value', array('name'=>'version'))) {
-        purge_all_caches();
-        redirect(new moodle_url('/admin/index.php'), 'Config cache inconsistency detected, resetting caches...');
-    }
+if ($CFG->version != $DB->get_field('config', 'value', array('name'=>'version'))) {
+    purge_all_caches();
+    redirect(new moodle_url('/admin/index.php'), 'Config cache inconsistency detected, resetting caches...');
 }
 
-if ($version > $CFG->version) {  // upgrade
+if (!$cache and $version > $CFG->version) {  // upgrade
     // We purge all of MUC's caches here.
     // Caches are disabled for upgrade by CACHE_DISABLE_ALL so we must set the first arg to true.
     // This ensures a real config object is loaded and the stores will be purged.
@@ -245,6 +255,7 @@ if ($version > $CFG->version) {  // upgrade
         $PAGE->set_title($stradministration);
         $PAGE->set_cacheable(false);
 
+        /** @var core_admin_renderer $output */
         $output = $PAGE->get_renderer('core', 'admin');
         echo $output->upgrade_stale_php_files_page();
         die();
@@ -260,6 +271,7 @@ if ($version > $CFG->version) {  // upgrade
         $PAGE->set_heading($strdatabasechecking);
         $PAGE->set_cacheable(false);
 
+        /** @var core_admin_renderer $output */
         $output = $PAGE->get_renderer('core', 'admin');
         echo $output->upgrade_confirm_page($a->newversion, $maturity);
         die();
@@ -274,6 +286,7 @@ if ($version > $CFG->version) {  // upgrade
         $PAGE->set_heading($strcurrentrelease);
         $PAGE->set_cacheable(false);
 
+        /** @var core_admin_renderer $output */
         $output = $PAGE->get_renderer('core', 'admin');
         echo $output->upgrade_environment_page($release, $envstatus, $environment_results);
         die();
@@ -288,10 +301,12 @@ if ($version > $CFG->version) {  // upgrade
 
         $reloadurl = new moodle_url('/admin/index.php', array('confirmupgrade' => 1, 'confirmrelease' => 1));
 
+        /** @var core_admin_renderer $output */
+        $output = $PAGE->get_renderer('core', 'admin');
+
         // check plugin dependencies first
         $failed = array();
         if (!plugin_manager::instance()->all_plugins_ok($version, $failed)) {
-            $output = $PAGE->get_renderer('core', 'admin');
             echo $output->unsatisfied_dependencies_page($version, $failed, $reloadurl);
             die();
         }
@@ -305,8 +320,6 @@ if ($version > $CFG->version) {  // upgrade
             redirect($reloadurl);
         }
 
-        $output = $PAGE->get_renderer('core', 'admin');
-
         $deployer = available_update_deployer::instance();
         if ($deployer->enabled()) {
             $deployer->initialize($reloadurl, $reloadurl);
@@ -333,15 +346,15 @@ if ($version > $CFG->version) {  // upgrade
 }
 
 // Updated human-readable release version if necessary
-if ($release <> $CFG->release) {  // Update the release version
+if (!$cache and $release <> $CFG->release) {  // Update the release version
     set_config('release', $release);
 }
 
-if ($branch <> $CFG->branch) {  // Update the branch
+if (!$cache and $branch <> $CFG->branch) {  // Update the branch
     set_config('branch', $branch);
 }
 
-if (moodle_needs_upgrading()) {
+if (!$cache and moodle_needs_upgrading()) {
     if (!$PAGE->headerprinted) {
         // means core upgrade or installation was not already done
         if (!$confirmplugins) {
@@ -450,8 +463,8 @@ if (during_initial_install()) {
 
 // Now we can be sure everything was upgraded and caches work fine,
 // redirect if necessary to make sure caching is enabled.
-if (!$cache and !optional_param('sesskey', '', PARAM_RAW)) {
-    redirect(new moodle_url($PAGE->url, array('cache' => 1)));
+if (!$cache) {
+    redirect(new moodle_url('/admin/index.php', array('cache' => 1)));
 }
 
 // Check for valid admin user - no guest autologin
@@ -473,17 +486,6 @@ if (!empty($id) and $id == $CFG->siteidentifier) {
     set_config('registered', time());
 }
 
-// Check if we are returning from an add-on installation request at moodle.org/plugins
-if (!is_null($newaddonreq)) {
-    if (!empty($CFG->disableonclickaddoninstall)) {
-        // The feature is disabled in config.php, ignore the request.
-    } else {
-        redirect(new moodle_url('/admin/tool/installaddon/index.php', array(
-            'installaddonrequest' => $newaddonreq,
-            'confirm' => 0)));
-    }
-}
-
 // setup critical warnings before printing admin tree block
 $insecuredataroot = is_dataroot_insecure(true);
 $SESSION->admin_critical_warning = ($insecuredataroot==INSECURE_DATAROOT_ERROR);
index 8ceba12..a092e00 100644 (file)
@@ -38,9 +38,6 @@ require_once($CFG->libdir . '/adminlib.php');
 require_once($CFG->libdir . '/pluginlib.php');
 require_once($CFG->libdir . '/filelib.php');
 
-admin_externalpage_setup('pluginsoverview');
-require_capability('moodle/site:config', context_system::instance());
-
 $fetchremote = optional_param('fetchremote', false, PARAM_BOOL);
 $updatesonly = optional_param('updatesonly', false, PARAM_BOOL);
 $contribonly = optional_param('contribonly', false, PARAM_BOOL);
@@ -48,12 +45,30 @@ $uninstall = optional_param('uninstall', '', PARAM_COMPONENT);
 $delete = optional_param('delete', '', PARAM_COMPONENT);
 $confirmed = optional_param('confirm', false, PARAM_BOOL);
 
-$output = $PAGE->get_renderer('core', 'admin');
+// NOTE: do not use admin_externalpage_setup() here because it loads
+//       full admin tree which is not possible during uninstallation.
+
+require_login();
+$syscontext = context_system::instance();
+require_capability('moodle/site:config', $syscontext);
 
 $pluginman = plugin_manager::instance();
 
 if ($uninstall) {
     require_sesskey();
+
+    if (!$confirmed) {
+        admin_externalpage_setup('pluginsoverview');
+    } else {
+        $PAGE->set_url('/admin/plugins.php');
+        $PAGE->set_context($syscontext);
+        $PAGE->set_pagelayout('maintenance');
+        $PAGE->set_popup_notification_allowed(false);
+    }
+
+    /** @var core_admin_renderer $output */
+    $output = $PAGE->get_renderer('core', 'admin');
+
     $pluginfo = $pluginman->get_plugin_info($uninstall);
 
     // Make sure we know the plugin.
@@ -104,6 +119,15 @@ if ($uninstall) {
 
 if ($delete and $confirmed) {
     require_sesskey();
+
+    $PAGE->set_url('/admin/plugins.php');
+    $PAGE->set_context($syscontext);
+    $PAGE->set_pagelayout('maintenance');
+    $PAGE->set_popup_notification_allowed(false);
+
+    /** @var core_admin_renderer $output */
+    $output = $PAGE->get_renderer('core', 'admin');
+
     $pluginfo = $pluginman->get_plugin_info($delete);
 
     // Make sure we know the plugin.
@@ -143,9 +167,15 @@ if ($delete and $confirmed) {
     if (function_exists('opcache_reset')) {
         opcache_reset();
     }
-    redirect($PAGE->url);
+    // We need to execute upgrade to make sure everything including caches is up to date.
+    redirect(new moodle_url('/admin/index.php'));
 }
 
+admin_externalpage_setup('pluginsoverview');
+
+/** @var core_admin_renderer $output */
+$output = $PAGE->get_renderer('core', 'admin');
+
 $checker = available_update_checker::instance();
 
 // Filtering options.
index 68cdb49..fad8eff 100644 (file)
@@ -140,12 +140,13 @@ class core_admin_renderer extends plugin_renderer_base {
     public function upgrade_confirm_page($strnewversion, $maturity) {
         $output = '';
 
-        $continueurl = new moodle_url('index.php', array('confirmupgrade' => 1));
-        $cancelurl = new moodle_url('index.php');
+        $continueurl = new moodle_url('/admin/index.php', array('confirmupgrade' => 1));
+        $continue = new single_button($continueurl, get_string('continue'), 'get');
+        $cancelurl = new moodle_url('/admin/index.php');
 
         $output .= $this->header();
         $output .= $this->maturity_warning($maturity);
-        $output .= $this->confirm(get_string('upgradesure', 'admin', $strnewversion), $continueurl, $cancelurl);
+        $output .= $this->confirm(get_string('upgradesure', 'admin', $strnewversion), $continue, $cancelurl);
         $output .= $this->footer();
 
         return $output;
@@ -412,6 +413,10 @@ class core_admin_renderer extends plugin_renderer_base {
 
         $pluginname = $pluginman->plugin_name($pluginfo->component);
 
+        // Do not show navigation here, they must click one of the buttons.
+        $this->page->set_pagelayout('maintenance');
+        $this->page->set_cacheable(false);
+
         $output .= $this->output->header();
         $output .= $this->output->heading(get_string('uninstalling', 'core_plugin', array('name' => $pluginname)));
 
@@ -425,7 +430,8 @@ class core_admin_renderer extends plugin_renderer_base {
                 'uninstalldeleteconfirmexternal');
         }
 
-        $output .= $this->output->confirm($confirm, $continueurl, $this->page->url);
+        // After any uninstall we must execute full upgrade to finish the cleanup!
+        $output .= $this->output->confirm($confirm, $continueurl, new moodle_url('/admin/index.php'));
         $output .= $this->output->footer();
 
         return $output;
@@ -442,7 +448,7 @@ class core_admin_renderer extends plugin_renderer_base {
     public function plugin_uninstall_results_page(plugin_manager $pluginman, plugininfo_base $pluginfo, progress_trace_buffer $progress) {
         $output = '';
 
-        $pluginname = $pluginman->plugin_name($pluginfo->component);
+        $pluginname = $pluginfo->component;
 
         $output .= $this->output->header();
         $output .= $this->output->heading(get_string('uninstalling', 'core_plugin', array('name' => $pluginname)));
@@ -451,7 +457,7 @@ class core_admin_renderer extends plugin_renderer_base {
 
         $output .= $this->output->box(get_string('uninstalldelete', 'core_plugin',
             array('name' => $pluginname, 'rootdir' => $pluginfo->rootdir)), 'generalbox uninstalldelete');
-        $output .= $this->output->continue_button($this->page->url);
+        $output .= $this->output->continue_button(new moodle_url('/admin/index.php'));
         $output .= $this->output->footer();
 
         return $output;
@@ -708,7 +714,7 @@ class core_admin_renderer extends plugin_renderer_base {
 
         if (!$registered) {
 
-            $registerbutton = $this->single_button(new moodle_url('registration/register.php',
+            $registerbutton = $this->single_button(new moodle_url('/admin/registration/register.php',
                     array('huburl' =>  HUB_MOODLEORGHUBURL, 'hubname' => 'Moodle.org')),
                     get_string('register', 'admin'));
 
index beed421..20b3370 100644 (file)
 // You should have received a copy of the GNU General Public License
 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
 
+/**
+ * Backend generic code.
+ *
+ * @package tool_generator
+ * @copyright 2013 The Open University
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
 defined('MOODLE_INTERNAL') || die();
 
 /**
- * Backend code for the 'make large course' tool.
+ * Backend generic code for all tool_generator commands.
  *
+ * @abstract
  * @package tool_generator
  * @copyright 2013 The Open University
  * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
-class tool_generator_backend {
+abstract class tool_generator_backend {
     /**
      * @var int Lowest (smallest) size index
      */
@@ -38,126 +47,56 @@ class tool_generator_backend {
     const DEFAULT_SIZE = 3;
 
     /**
-     * @var array Number of sections in course
-     */
-    private static $paramsections = array(1, 10, 100, 500, 1000, 2000);
-    /**
-     * @var array Number of Page activities in course
-     */
-    private static $parampages = array(1, 50, 200, 1000, 5000, 10000);
-    /**
-     * @var array Number of students enrolled in course
-     */
-    private static $paramusers = array(1, 100, 1000, 10000, 50000, 100000);
-    /**
-     * Total size of small files: 1KB, 1MB, 10MB, 100MB, 1GB, 2GB.
-     *
-     * @var array Number of small files created in a single file activity
-     */
-    private static $paramsmallfilecount = array(1, 64, 128, 1024, 16384, 32768);
-    /**
-     * @var array Size of small files (to make the totals into nice numbers)
-     */
-    private static $paramsmallfilesize = array(1024, 16384, 81920, 102400, 65536, 65536);
-    /**
-     * Total size of big files: 8KB, 8MB, 80MB, 800MB, 8GB, 16GB.
-     *
-     * @var array Number of big files created as individual file activities
-     */
-    private static $parambigfilecount = array(1, 2, 5, 10, 10, 10);
-    /**
-     * @var array Size of each large file
-     */
-    private static $parambigfilesize = array(8192, 4194304, 16777216, 83886080,
-            858993459, 1717986918);
-    /**
-     * @var array Number of forum discussions
-     */
-    private static $paramforumdiscussions = array(1, 10, 100, 500, 1000, 2000);
-    /**
-     * @var array Number of forum posts per discussion
-     */
-    private static $paramforumposts = array(2, 2, 5, 10, 10, 10);
-
-    /**
-     * @var string Course shortname
-     */
-    private $shortname;
-
-    /**
-     * @var int Size code (index in the above arrays)
+     * @var bool True if we want a fixed dataset or false to generate random data
      */
-    private $size;
+    protected $fixeddataset;
 
     /**
      * @var bool True if displaying progress
      */
-    private $progress;
-
-    /**
-     * @var testing_data_generator Data generator
-     */
-    private $generator;
-
-    /**
-     * @var stdClass Course object
-     */
-    private $course;
+    protected $progress;
 
     /**
      * @var int Epoch time at which last dot was displayed
      */
-    private $lastdot;
+    protected $lastdot;
 
     /**
      * @var int Epoch time at which last percentage was displayed
      */
-    private $lastpercentage;
+    protected $lastpercentage;
 
     /**
      * @var int Epoch time at which current step (current set of dots) started
      */
-    private $starttime;
+    protected $starttime;
 
     /**
-     * @var array Array from test user number (1...N) to userid in database
+     * @var int Size code (index in the above arrays)
      */
-    private $userids;
+    protected $size;
 
     /**
-     * Constructs object ready to create course.
+     * Generic generator class
      *
-     * @param string $shortname Course shortname
      * @param int $size Size as numeric index
+     * @param bool $fixeddataset To use fixed or random data
      * @param bool $progress True if progress information should be displayed
-     * @return int Course id
      * @throws coding_exception If parameters are invalid
      */
-    public function __construct($shortname, $size, $progress = true) {
+    public function __construct($size, $fixeddataset = false, $progress = true) {
+
         // Check parameter.
         if ($size < self::MIN_SIZE || $size > self::MAX_SIZE) {
             throw new coding_exception('Invalid size');
         }
 
         // Set parameters.
-        $this->shortname = $shortname;
         $this->size = $size;
+        $this->fixeddataset = $fixeddataset;
         $this->progress = $progress;
     }
 
-    /**
-     * Gets a list of size choices supported by this backend.
-     *
-     * @return array List of size (int) => text description for display
-     */
-    public static function get_size_choices() {
-        $options = array();
-        for ($size = self::MIN_SIZE; $size <= self::MAX_SIZE; $size++) {
-            $options[$size] = get_string('size_' . $size, 'tool_generator');
-        }
-        return $options;
-    }
-
     /**
      * Converts a size name into the numeric constant.
      *
@@ -174,367 +113,13 @@ class tool_generator_backend {
         throw new coding_exception("Unknown size name '$sizename'");
     }
 
-    /**
-     * Checks that a shortname is available (unused).
-     *
-     * @param string $shortname Proposed course shortname
-     * @return string An error message if the name is unavailable or '' if OK
-     */
-    public static function check_shortname_available($shortname) {
-        global $DB;
-        $fullname = $DB->get_field('course', 'fullname',
-                array('shortname' => $shortname), IGNORE_MISSING);
-        if ($fullname !== false) {
-            // I wanted to throw an exception here but it is not possible to
-            // use strings from moodle.php in exceptions, and I didn't want
-            // to duplicate the string in tool_generator, so I changed this to
-            // not use exceptions.
-            return get_string('shortnametaken', 'moodle', $fullname);
-        }
-        return '';
-    }
-
-    /**
-     * Runs the entire 'make' process.
-     *
-     * @return int Course id
-     */
-    public function make() {
-        global $DB, $CFG;
-        require_once($CFG->dirroot . '/lib/phpunit/classes/util.php');
-
-        raise_memory_limit(MEMORY_EXTRA);
-
-        if ($this->progress && !CLI_SCRIPT) {
-            echo html_writer::start_tag('ul');
-        }
-
-        $entirestart = microtime(true);
-
-        // Start transaction.
-        $transaction = $DB->start_delegated_transaction();
-
-        // Get generator.
-        $this->generator = phpunit_util::get_data_generator();
-
-        // Make course.
-        $this->course = $this->create_course();
-        $this->create_users();
-        $this->create_pages();
-        $this->create_small_files();
-        $this->create_big_files();
-        $this->create_forum();
-
-        // Log total time.
-        $this->log('complete', round(microtime(true) - $entirestart, 1));
-
-        if ($this->progress && !CLI_SCRIPT) {
-            echo html_writer::end_tag('ul');
-        }
-
-        // Commit transaction and finish.
-        $transaction->allow_commit();
-        return $this->course->id;
-    }
-
-    /**
-     * Creates the actual course.
-     *
-     * @return stdClass Course record
-     */
-    private function create_course() {
-        $this->log('createcourse', $this->shortname);
-        $courserecord = array('shortname' => $this->shortname,
-                'fullname' => get_string('fullname', 'tool_generator',
-                    array('size' => get_string('shortsize_' . $this->size, 'tool_generator'))),
-                'numsections' => self::$paramsections[$this->size]);
-        return $this->generator->create_course($courserecord, array('createsections' => true));
-    }
-
-    /**
-     * Creates a number of user accounts and enrols them on the course.
-     * Note: Existing user accounts that were created by this system are
-     * reused if available.
-     */
-    private function create_users() {
-        global $DB;
-
-        // Work out total number of users.
-        $count = self::$paramusers[$this->size];
-
-        // Get existing users in order. We will 'fill up holes' in this up to
-        // the required number.
-        $this->log('checkaccounts', $count);
-        $nextnumber = 1;
-        $rs = $DB->get_recordset_select('user', $DB->sql_like('username', '?'),
-                array('tool_generator_%'), 'username', 'id, username');
-        foreach ($rs as $rec) {
-            // Extract number from username.
-            $matches = array();
-            if (!preg_match('~^tool_generator_([0-9]{6})$~', $rec->username, $matches)) {
-                continue;
-            }
-            $number = (int)$matches[1];
-
-            // Create missing users in range up to this.
-            if ($number != $nextnumber) {
-                $this->create_user_accounts($nextnumber, min($number - 1, $count));
-            } else {
-                $this->userids[$number] = (int)$rec->id;
-            }
-
-            // Stop if we've got enough users.
-            $nextnumber = $number + 1;
-            if ($number >= $count) {
-                break;
-            }
-        }
-        $rs->close();
-
-        // Create users from end of existing range.
-        if ($nextnumber <= $count) {
-            $this->create_user_accounts($nextnumber, $count);
-        }
-
-        // Assign all users to course.
-        $this->log('enrol', $count, true);
-
-        $enrolplugin = enrol_get_plugin('manual');
-        $instances = enrol_get_instances($this->course->id, true);
-        foreach ($instances as $instance) {
-            if ($instance->enrol === 'manual') {
-                break;
-            }
-        }
-        if ($instance->enrol !== 'manual') {
-            throw new coding_exception('No manual enrol plugin in course');
-        }
-        $role = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST);
-
-        for ($number = 1; $number <= $count; $number++) {
-            // Enrol user.
-            $enrolplugin->enrol_user($instance, $this->userids[$number], $role->id);
-            $this->dot($number, $count);
-        }
-
-        $this->end_log();
-    }
-
-    /**
-     * Creates user accounts with a numeric range.
-     *
-     * @param int $first Number of first user
-     * @param int $last Number of last user
-     */
-    private function create_user_accounts($first, $last) {
-        $this->log('createaccounts', (object)array('from' => $first, 'to' => $last), true);
-        $count = $last - $first + 1;
-        $done = 0;
-        for ($number = $first; $number <= $last; $number++, $done++) {
-            // Work out username with 6-digit number.
-            $textnumber = (string)$number;
-            while (strlen($textnumber) < 6) {
-                $textnumber = '0' . $textnumber;
-            }
-            $username = 'tool_generator_' . $textnumber;
-
-            // Create user account.
-            $record = array('firstname' => get_string('firstname', 'tool_generator'),
-                    'lastname' => $number, 'username' => $username);
-            $user = $this->generator->create_user($record);
-            $this->userids[$number] = (int)$user->id;
-            $this->dot($done, $count);
-        }
-        $this->end_log();
-    }
-
-    /**
-     * Creates a number of Page activities.
-     */
-    private function create_pages() {
-        // Set up generator.
-        $pagegenerator = $this->generator->get_plugin_generator('mod_page');
-
-        // Create pages.
-        $number = self::$parampages[$this->size];
-        $this->log('createpages', $number, true);
-        for ($i=0; $i<$number; $i++) {
-            $record = array('course' => $this->course->id);
-            $options = array('section' => $this->get_random_section());
-            $pagegenerator->create_instance($record, $options);
-            $this->dot($i, $number);
-        }
-
-        $this->end_log();
-    }
-
-    /**
-     * Creates one resource activity with a lot of small files.
-     */
-    private function create_small_files() {
-        $count = self::$paramsmallfilecount[$this->size];
-        $this->log('createsmallfiles', $count, true);
-
-        // Create resource with default textfile only.
-        $resourcegenerator = $this->generator->get_plugin_generator('mod_resource');
-        $record = array('course' => $this->course->id,
-                'name' => get_string('smallfiles', 'tool_generator'));
-        $options = array('section' => 0);
-        $resource = $resourcegenerator->create_instance($record, $options);
-
-        // Add files.
-        $fs = get_file_storage();
-        $context = context_module::instance($resource->cmid);
-        $filerecord = array('component' => 'mod_resource', 'filearea' => 'content',
-                'contextid' => $context->id, 'itemid' => 0, 'filepath' => '/');
-        for ($i = 0; $i < $count; $i++) {
-            $filerecord['filename'] = 'smallfile' . $i . '.dat';
-
-            // Generate random binary data (different for each file so it
-            // doesn't compress unrealistically).
-            $data = self::get_random_binary(self::$paramsmallfilesize[$this->size]);
-
-            $fs->create_file_from_string($filerecord, $data);
-            $this->dot($i, $count);
-        }
-
-        $this->end_log();
-    }
-
-    /**
-     * Creates a string of random binary data. The start of the string includes
-     * the current time, in an attempt to avoid large-scale repetition.
-     *
-     * @param int $length Number of bytes
-     * @return Random data
-     */
-    private static function get_random_binary($length) {
-        $data = microtime(true);
-        if (strlen($data) > $length) {
-            // Use last digits of data.
-            return substr($data, -$length);
-        }
-        $length -= strlen($data);
-        for ($j=0; $j < $length; $j++) {
-            $data .= chr(rand(1, 255));
-        }
-        return $data;
-    }
-
-    /**
-     * Creates a number of resource activities with one big file each.
-     */
-    private function create_big_files() {
-        global $CFG;
-
-        // Work out how many files and how many blocks to use (up to 64KB).
-        $count = self::$parambigfilecount[$this->size];
-        $blocks = ceil(self::$parambigfilesize[$this->size] / 65536);
-        $blocksize = floor(self::$parambigfilesize[$this->size] / $blocks);
-
-        $this->log('createbigfiles', $count, true);
-
-        // Prepare temp area.
-        $tempfolder = make_temp_directory('tool_generator');
-        $tempfile = $tempfolder . '/' . rand();
-
-        // Create resources and files.
-        $fs = get_file_storage();
-        $resourcegenerator = $this->generator->get_plugin_generator('mod_resource');
-        for ($i = 0; $i < $count; $i++) {
-            // Create resource.
-            $record = array('course' => $this->course->id,
-                    'name' => get_string('bigfile', 'tool_generator', $i));
-            $options = array('section' => $this->get_random_section());
-            $resource = $resourcegenerator->create_instance($record, $options);
-
-            // Write file.
-            $handle = fopen($tempfile, 'w');
-            if (!$handle) {
-                throw new coding_exception('Failed to open temporary file');
-            }
-            for ($j = 0; $j < $blocks; $j++) {
-                $data = self::get_random_binary($blocksize);
-                fwrite($handle, $data);
-                $this->dot($i * $blocks + $j, $count * $blocks);
-            }
-            fclose($handle);
-
-            // Add file.
-            $context = context_module::instance($resource->cmid);
-            $filerecord = array('component' => 'mod_resource', 'filearea' => 'content',
-                    'contextid' => $context->id, 'itemid' => 0, 'filepath' => '/',
-                    'filename' => 'bigfile' . $i . '.dat');
-            $fs->create_file_from_pathname($filerecord, $tempfile);
-        }
-
-        unlink($tempfile);
-        $this->end_log();
-    }
-
-    /**
-     * Creates one forum activity with a bunch of posts.
-     */
-    private function create_forum() {
-        global $DB;
-
-        $discussions = self::$paramforumdiscussions[$this->size];
-        $posts = self::$paramforumposts[$this->size];
-        $totalposts = $discussions * $posts;
-
-        $this->log('createforum', $totalposts, true);
-
-        // Create empty forum.
-        $forumgenerator = $this->generator->get_plugin_generator('mod_forum');
-        $record = array('course' => $this->course->id,
-                'name' => get_string('pluginname', 'forum'));
-        $options = array('section' => 0);
-        $forum = $forumgenerator->create_instance($record, $options);
-
-        // Add discussions and posts.
-        $sofar = 0;
-        for ($i=0; $i < $discussions; $i++) {
-            $record = array('forum' => $forum->id, 'course' => $this->course->id,
-                    'userid' => $this->get_random_user());
-            $discussion = $forumgenerator->create_discussion($record);
-            $parentid = $DB->get_field('forum_posts', 'id', array('discussion' => $discussion->id), MUST_EXIST);
-            $sofar++;
-            for ($j=0; $j < $posts - 1; $j++, $sofar++) {
-                $record = array('discussion' => $discussion->id,
-                        'userid' => $this->get_random_user(), 'parent' => $parentid);
-                $forumgenerator->create_post($record);
-                $this->dot($sofar, $totalposts);
-            }
-        }
-
-        $this->end_log();
-    }
-
-    /**
-     * Gets a random section number.
-     *
-     * @return int A section number from 1 to the number of sections
-     */
-    private function get_random_section() {
-        return rand(1, self::$paramsections[$this->size]);
-    }
-
-    /**
-     * Gets a random user id.
-     *
-     * @return int A user id for a random created user
-     */
-    private function get_random_user() {
-        return $this->userids[rand(1, self::$paramusers[$this->size])];
-    }
-
     /**
      * Displays information as part of progress.
      * @param string $langstring Part of langstring (after progress_)
      * @param mixed $a Optional lang string parameters
      * @param bool $leaveopen If true, doesn't close LI tag (ready for dots)
      */
-    private function log($langstring, $a = null, $leaveopen = false) {
+    protected function log($langstring, $a = null, $leaveopen = false) {
         if (!$this->progress) {
             return;
         }
@@ -564,7 +149,7 @@ class tool_generator_backend {
      * @param int $number Number of completed items
      * @param int $total Total number of items to complete
      */
-    private function dot($number, $total) {
+    protected function dot($number, $total) {
         if (!$this->progress) {
             return;
         }
@@ -592,7 +177,7 @@ class tool_generator_backend {
     /**
      * Ends a log string that was started using log function with $leaveopen.
      */
-    private function end_log() {
+    protected function end_log() {
         if (!$this->progress) {
             return;
         }
@@ -603,4 +188,5 @@ class tool_generator_backend {
             echo html_writer::end_tag('li');
         }
     }
+
 }
diff --git a/admin/tool/generator/classes/course_backend.php b/admin/tool/generator/classes/course_backend.php
new file mode 100644 (file)
index 0000000..07e1e7b
--- /dev/null
@@ -0,0 +1,507 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * tool_generator course backend code.
+ *
+ * @package tool_generator
+ * @copyright 2013 The Open University
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Backend code for the 'make large course' tool.
+ *
+ * @package tool_generator
+ * @copyright 2013 The Open University
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class tool_generator_course_backend extends tool_generator_backend {
+    /**
+     * @var array Number of sections in course
+     */
+    private static $paramsections = array(1, 10, 100, 500, 1000, 2000);
+    /**
+     * @var array Number of Page activities in course
+     */
+    private static $parampages = array(1, 50, 200, 1000, 5000, 10000);
+    /**
+     * @var array Number of students enrolled in course
+     */
+    private static $paramusers = array(1, 100, 1000, 10000, 50000, 100000);
+    /**
+     * Total size of small files: 1KB, 1MB, 10MB, 100MB, 1GB, 2GB.
+     *
+     * @var array Number of small files created in a single file activity
+     */
+    private static $paramsmallfilecount = array(1, 64, 128, 1024, 16384, 32768);
+    /**
+     * @var array Size of small files (to make the totals into nice numbers)
+     */
+    private static $paramsmallfilesize = array(1024, 16384, 81920, 102400, 65536, 65536);
+    /**
+     * Total size of big files: 8KB, 8MB, 80MB, 800MB, 8GB, 16GB.
+     *
+     * @var array Number of big files created as individual file activities
+     */
+    private static $parambigfilecount = array(1, 2, 5, 10, 10, 10);
+    /**
+     * @var array Size of each large file
+     */
+    private static $parambigfilesize = array(8192, 4194304, 16777216, 83886080,
+            858993459, 1717986918);
+    /**
+     * @var array Number of forum discussions
+     */
+    private static $paramforumdiscussions = array(1, 10, 100, 500, 1000, 2000);
+    /**
+     * @var array Number of forum posts per discussion
+     */
+    private static $paramforumposts = array(2, 2, 5, 10, 10, 10);
+
+    /**
+     * @var string Course shortname
+     */
+    private $shortname;
+
+    /**
+     * @var testing_data_generator Data generator
+     */
+    protected $generator;
+
+    /**
+     * @var stdClass Course object
+     */
+    private $course;
+
+    /**
+     * @var array Array from test user number (1...N) to userid in database
+     */
+    private $userids;
+
+    /**
+     * Constructs object ready to create course.
+     *
+     * @param string $shortname Course shortname
+     * @param int $size Size as numeric index
+     * @param bool $fixeddataset To use fixed or random data
+     * @param bool $progress True if progress information should be displayed
+     * @return int Course id
+     */
+    public function __construct($shortname, $size, $fixeddataset = false, $progress = true) {
+
+        // Set parameters.
+        $this->shortname = $shortname;
+
+        parent::__construct($size, $fixeddataset, $progress);
+    }
+
+    /**
+     * Gets a list of size choices supported by this backend.
+     *
+     * @return array List of size (int) => text description for display
+     */
+    public static function get_size_choices() {
+        $options = array();
+        for ($size = self::MIN_SIZE; $size <= self::MAX_SIZE; $size++) {
+            $options[$size] = get_string('coursesize_' . $size, 'tool_generator');
+        }
+        return $options;
+    }
+
+    /**
+     * Checks that a shortname is available (unused).
+     *
+     * @param string $shortname Proposed course shortname
+     * @return string An error message if the name is unavailable or '' if OK
+     */
+    public static function check_shortname_available($shortname) {
+        global $DB;
+        $fullname = $DB->get_field('course', 'fullname',
+                array('shortname' => $shortname), IGNORE_MISSING);
+        if ($fullname !== false) {
+            // I wanted to throw an exception here but it is not possible to
+            // use strings from moodle.php in exceptions, and I didn't want
+            // to duplicate the string in tool_generator, so I changed this to
+            // not use exceptions.
+            return get_string('shortnametaken', 'moodle', $fullname);
+        }
+        return '';
+    }
+
+    /**
+     * Runs the entire 'make' process.
+     *
+     * @return int Course id
+     */
+    public function make() {
+        global $DB, $CFG;
+        require_once($CFG->dirroot . '/lib/phpunit/classes/util.php');
+
+        raise_memory_limit(MEMORY_EXTRA);
+
+        if ($this->progress && !CLI_SCRIPT) {
+            echo html_writer::start_tag('ul');
+        }
+
+        $entirestart = microtime(true);
+
+        // Start transaction.
+        $transaction = $DB->start_delegated_transaction();
+
+        // Get generator.
+        $this->generator = phpunit_util::get_data_generator();
+
+        // Make course.
+        $this->course = $this->create_course();
+        $this->create_users();
+        $this->create_pages();
+        $this->create_small_files();
+        $this->create_big_files();
+        $this->create_forum();
+
+        // Log total time.
+        $this->log('coursecompleted', round(microtime(true) - $entirestart, 1));
+
+        if ($this->progress && !CLI_SCRIPT) {
+            echo html_writer::end_tag('ul');
+        }
+
+        // Commit transaction and finish.
+        $transaction->allow_commit();
+        return $this->course->id;
+    }
+
+    /**
+     * Creates the actual course.
+     *
+     * @return stdClass Course record
+     */
+    private function create_course() {
+        $this->log('createcourse', $this->shortname);
+        $courserecord = array('shortname' => $this->shortname,
+                'fullname' => get_string('fullname', 'tool_generator',
+                    array('size' => get_string('shortsize_' . $this->size, 'tool_generator'))),
+                'numsections' => self::$paramsections[$this->size]);
+        return $this->generator->create_course($courserecord, array('createsections' => true));
+    }
+
+    /**
+     * Creates a number of user accounts and enrols them on the course.
+     * Note: Existing user accounts that were created by this system are
+     * reused if available.
+     */
+    private function create_users() {
+        global $DB;
+
+        // Work out total number of users.
+        $count = self::$paramusers[$this->size];
+
+        // Get existing users in order. We will 'fill up holes' in this up to
+        // the required number.
+        $this->log('checkaccounts', $count);
+        $nextnumber = 1;
+        $rs = $DB->get_recordset_select('user', $DB->sql_like('username', '?'),
+                array('tool_generator_%'), 'username', 'id, username');
+        foreach ($rs as $rec) {
+            // Extract number from username.
+            $matches = array();
+            if (!preg_match('~^tool_generator_([0-9]{6})$~', $rec->username, $matches)) {
+                continue;
+            }
+            $number = (int)$matches[1];
+
+            // Create missing users in range up to this.
+            if ($number != $nextnumber) {
+                $this->create_user_accounts($nextnumber, min($number - 1, $count));
+            } else {
+                $this->userids[$number] = (int)$rec->id;
+            }
+
+            // Stop if we've got enough users.
+            $nextnumber = $number + 1;
+            if ($number >= $count) {
+                break;
+            }
+        }
+        $rs->close();
+
+        // Create users from end of existing range.
+        if ($nextnumber <= $count) {
+            $this->create_user_accounts($nextnumber, $count);
+        }
+
+        // Assign all users to course.
+        $this->log('enrol', $count, true);
+
+        $enrolplugin = enrol_get_plugin('manual');
+        $instances = enrol_get_instances($this->course->id, true);
+        foreach ($instances as $instance) {
+            if ($instance->enrol === 'manual') {
+                break;
+            }
+        }
+        if ($instance->enrol !== 'manual') {
+            throw new coding_exception('No manual enrol plugin in course');
+        }
+        $role = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST);
+
+        for ($number = 1; $number <= $count; $number++) {
+            // Enrol user.
+            $enrolplugin->enrol_user($instance, $this->userids[$number], $role->id);
+            $this->dot($number, $count);
+        }
+
+        // Sets the pointer at the beginning to be aware of the users we use.
+        reset($this->userids);
+
+        $this->end_log();
+    }
+
+    /**
+     * Creates user accounts with a numeric range.
+     *
+     * @param int $first Number of first user
+     * @param int $last Number of last user
+     */
+    private function create_user_accounts($first, $last) {
+        $this->log('createaccounts', (object)array('from' => $first, 'to' => $last), true);
+        $count = $last - $first + 1;
+        $done = 0;
+        for ($number = $first; $number <= $last; $number++, $done++) {
+            // Work out username with 6-digit number.
+            $textnumber = (string)$number;
+            while (strlen($textnumber) < 6) {
+                $textnumber = '0' . $textnumber;
+            }
+            $username = 'tool_generator_' . $textnumber;
+
+            // Create user account.
+            $record = array('firstname' => get_string('firstname', 'tool_generator'),
+                    'lastname' => $number, 'username' => $username);
+            $user = $this->generator->create_user($record);
+            $this->userids[$number] = (int)$user->id;
+            $this->dot($done, $count);
+        }
+        $this->end_log();
+    }
+
+    /**
+     * Creates a number of Page activities.
+     */
+    private function create_pages() {
+        // Set up generator.
+        $pagegenerator = $this->generator->get_plugin_generator('mod_page');
+
+        // Create pages.
+        $number = self::$parampages[$this->size];
+        $this->log('createpages', $number, true);
+        for ($i=0; $i<$number; $i++) {
+            $record = array('course' => $this->course->id);
+            $options = array('section' => $this->get_target_section());
+            $pagegenerator->create_instance($record, $options);
+            $this->dot($i, $number);
+        }
+
+        $this->end_log();
+    }
+
+    /**
+     * Creates one resource activity with a lot of small files.
+     */
+    private function create_small_files() {
+        $count = self::$paramsmallfilecount[$this->size];
+        $this->log('createsmallfiles', $count, true);
+
+        // Create resource with default textfile only.
+        $resourcegenerator = $this->generator->get_plugin_generator('mod_resource');
+        $record = array('course' => $this->course->id,
+                'name' => get_string('smallfiles', 'tool_generator'));
+        $options = array('section' => 0);
+        $resource = $resourcegenerator->create_instance($record, $options);
+
+        // Add files.
+        $fs = get_file_storage();
+        $context = context_module::instance($resource->cmid);
+        $filerecord = array('component' => 'mod_resource', 'filearea' => 'content',
+                'contextid' => $context->id, 'itemid' => 0, 'filepath' => '/');
+        for ($i = 0; $i < $count; $i++) {
+            $filerecord['filename'] = 'smallfile' . $i . '.dat';
+
+            // Generate random binary data (different for each file so it
+            // doesn't compress unrealistically).
+            $data = self::get_random_binary(self::$paramsmallfilesize[$this->size]);
+
+            $fs->create_file_from_string($filerecord, $data);
+            $this->dot($i, $count);
+        }
+
+        $this->end_log();
+    }
+
+    /**
+     * Creates a string of random binary data. The start of the string includes
+     * the current time, in an attempt to avoid large-scale repetition.
+     *
+     * @param int $length Number of bytes
+     * @return Random data
+     */
+    private static function get_random_binary($length) {
+        $data = microtime(true);
+        if (strlen($data) > $length) {
+            // Use last digits of data.
+            return substr($data, -$length);
+        }
+        $length -= strlen($data);
+        for ($j=0; $j < $length; $j++) {
+            $data .= chr(rand(1, 255));
+        }
+        return $data;
+    }
+
+    /**
+     * Creates a number of resource activities with one big file each.
+     */
+    private function create_big_files() {
+        global $CFG;
+
+        // Work out how many files and how many blocks to use (up to 64KB).
+        $count = self::$parambigfilecount[$this->size];
+        $blocks = ceil(self::$parambigfilesize[$this->size] / 65536);
+        $blocksize = floor(self::$parambigfilesize[$this->size] / $blocks);
+
+        $this->log('createbigfiles', $count, true);
+
+        // Prepare temp area.
+        $tempfolder = make_temp_directory('tool_generator');
+        $tempfile = $tempfolder . '/' . rand();
+
+        // Create resources and files.
+        $fs = get_file_storage();
+        $resourcegenerator = $this->generator->get_plugin_generator('mod_resource');
+        for ($i = 0; $i < $count; $i++) {
+            // Create resource.
+            $record = array('course' => $this->course->id,
+                    'name' => get_string('bigfile', 'tool_generator', $i));
+            $options = array('section' => $this->get_target_section());
+            $resource = $resourcegenerator->create_instance($record, $options);
+
+            // Write file.
+            $handle = fopen($tempfile, 'w');
+            if (!$handle) {
+                throw new coding_exception('Failed to open temporary file');
+            }
+            for ($j = 0; $j < $blocks; $j++) {
+                $data = self::get_random_binary($blocksize);
+                fwrite($handle, $data);
+                $this->dot($i * $blocks + $j, $count * $blocks);
+            }
+            fclose($handle);
+
+            // Add file.
+            $context = context_module::instance($resource->cmid);
+            $filerecord = array('component' => 'mod_resource', 'filearea' => 'content',
+                    'contextid' => $context->id, 'itemid' => 0, 'filepath' => '/',
+                    'filename' => 'bigfile' . $i . '.dat');
+            $fs->create_file_from_pathname($filerecord, $tempfile);
+        }
+
+        unlink($tempfile);
+        $this->end_log();
+    }
+
+    /**
+     * Creates one forum activity with a bunch of posts.
+     */
+    private function create_forum() {
+        global $DB;
+
+        $discussions = self::$paramforumdiscussions[$this->size];
+        $posts = self::$paramforumposts[$this->size];
+        $totalposts = $discussions * $posts;
+
+        $this->log('createforum', $totalposts, true);
+
+        // Create empty forum.
+        $forumgenerator = $this->generator->get_plugin_generator('mod_forum');
+        $record = array('course' => $this->course->id,
+                'name' => get_string('pluginname', 'forum'));
+        $options = array('section' => 0);
+        $forum = $forumgenerator->create_instance($record, $options);
+
+        // Add discussions and posts.
+        $sofar = 0;
+        for ($i=0; $i < $discussions; $i++) {
+            $record = array('forum' => $forum->id, 'course' => $this->course->id,
+                    'userid' => $this->get_target_user());
+            $discussion = $forumgenerator->create_discussion($record);
+            $parentid = $DB->get_field('forum_posts', 'id', array('discussion' => $discussion->id), MUST_EXIST);
+            $sofar++;
+            for ($j=0; $j < $posts - 1; $j++, $sofar++) {
+                $record = array('discussion' => $discussion->id,
+                        'userid' => $this->get_target_user(), 'parent' => $parentid);
+                $forumgenerator->create_post($record);
+                $this->dot($sofar, $totalposts);
+            }
+        }
+
+        $this->end_log();
+    }
+
+    /**
+     * Gets a section number.
+     *
+     * Depends on $this->fixeddataset.
+     *
+     * @return int A section number from 1 to the number of sections
+     */
+    private function get_target_section() {
+
+        if (!$this->fixeddataset) {
+            $key = rand(1, self::$paramsections[$this->size]);
+        } else {
+            // Using section 1.
+            $key = 1;
+        }
+
+        return $key;
+    }
+
+    /**
+     * Gets a user id.
+     *
+     * Depends on $this->fixeddataset.
+     *
+     * @return int A user id for a random created user
+     */
+    private function get_target_user() {
+
+        if (!$this->fixeddataset) {
+            $userid = $this->userids[rand(1, self::$paramusers[$this->size])];
+        } else if ($userid = current($this->userids)) {
+            // Moving pointer to the next user.
+            next($this->userids);
+        } else {
+            // Returning to the beginning if we reached the end.
+            $userid = reset($this->userids);
+        }
+
+        return $userid;
+    }
+
+}
index 25c8350..879364d 100644 (file)
@@ -31,8 +31,8 @@ class tool_generator_make_form extends moodleform {
         $mform = $this->_form;
 
         $mform->addElement('select', 'size', get_string('size', 'tool_generator'),
-                tool_generator_backend::get_size_choices());
-        $mform->setDefault('size', tool_generator_backend::DEFAULT_SIZE);
+                tool_generator_course_backend::get_size_choices());
+        $mform->setDefault('size', tool_generator_course_backend::DEFAULT_SIZE);
 
         $mform->addElement('text', 'shortname', get_string('shortnamecourse'));
         $mform->addRule('shortname', get_string('missingshortname'), 'required', null, 'client');
@@ -48,7 +48,7 @@ class tool_generator_make_form extends moodleform {
         // Check course doesn't already exist.
         if (!empty($data['shortname'])) {
             // Check shortname.
-            $error =  tool_generator_backend::check_shortname_available($data['shortname']);
+            $error =  tool_generator_course_backend::check_shortname_available($data['shortname']);
             if ($error) {
                 $errors['shortname'] = $error;
             }
diff --git a/admin/tool/generator/classes/site_backend.php b/admin/tool/generator/classes/site_backend.php
new file mode 100644 (file)
index 0000000..01bde97
--- /dev/null
@@ -0,0 +1,203 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * tool_generator site backend.
+ *
+ * @package tool_generator
+ * @copyright 2013 David Monllaó
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+/**
+ * Backend code for the site generator.
+ *
+ * @package tool_generator
+ * @copyright 2013 David Monllaó
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class tool_generator_site_backend extends tool_generator_backend {
+
+    /**
+     * @var string The course's shortname prefix.
+     */
+    const SHORTNAMEPREFIX = 'testcourse_';
+
+    /**
+     * @var bool If the debugging level checking was skipped.
+     */
+    protected $bypasscheck;
+
+    /**
+     * @var array Multidimensional array where the first level is the course size and the second the site size.
+     */
+    protected static $sitecourses = array(
+        array(2, 8, 64, 256, 1024, 4096),
+        array(1, 4, 8, 16, 32, 64),
+        array(0, 0, 1, 4, 8, 16),
+        array(0, 0, 0, 1, 0, 0),
+        array(0, 0, 0, 0, 1, 0),
+        array(0, 0, 0, 0, 0, 1)
+    );
+
+    /**
+     * Constructs object ready to make the site.
+     *
+     * @param int $size Size as numeric index
+     * @param bool $bypasscheck If debugging level checking was skipped.
+     * @param bool $fixeddataset To use fixed or random data
+     * @param bool $progress True if progress information should be displayed
+     * @return int Course id
+     */
+    public function __construct($size, $bypasscheck, $fixeddataset = false, $progress = true) {
+
+        // Set parameters.
+        $this->bypasscheck = $bypasscheck;
+
+        parent::__construct($size, $fixeddataset, $progress);
+    }
+
+    /**
+     * Gets a list of size choices supported by this backend.
+     *
+     * @return array List of size (int) => text description for display
+     */
+    public static function get_size_choices() {
+        $options = array();
+        for ($size = self::MIN_SIZE; $size <= self::MAX_SIZE; $size++) {
+            $options[$size] = get_string('sitesize_' . $size, 'tool_generator');
+        }
+        return $options;
+    }
+
+    /**
+     * Runs the entire 'make' process.
+     *
+     * @return int Course id
+     */
+    public function make() {
+        global $DB, $CFG;
+
+        raise_memory_limit(MEMORY_EXTRA);
+
+        if ($this->progress && !CLI_SCRIPT) {
+            echo html_writer::start_tag('ul');
+        }
+
+        $entirestart = microtime(true);
+
+        // Create courses.
+        $prevchdir = getcwd();
+        chdir($CFG->dirroot);
+        $ncourse = $this->get_last_testcourse_id();
+        foreach (self::$sitecourses as $coursesize => $ncourses) {
+            for ($i = 1; $i <= $ncourses[$this->size]; $i++) {
+                // Non language-dependant shortname.
+                $ncourse++;
+                $this->run_create_course(self::SHORTNAMEPREFIX . $ncourse, $coursesize);
+            }
+        }
+        chdir($prevchdir);
+
+        // Store last course id to return it (will be the bigger one).
+        $lastcourseid = $DB->get_field('course', 'id', array('shortname' => self::SHORTNAMEPREFIX . $ncourse));
+
+        // Log total time.
+        $this->log('sitecompleted', round(microtime(true) - $entirestart, 1));
+
+        if ($this->progress && !CLI_SCRIPT) {
+            echo html_writer::end_tag('ul');
+        }
+
+        return $lastcourseid;
+    }
+
+    /**
+     * Creates a course with the specified shortname, coursesize and the provided maketestsite options.
+     *
+     * @param string $shortname The course shortname
+     * @param int $coursesize One of the possible course sizes.
+     * @return void
+     */
+    protected function run_create_course($shortname, $coursesize) {
+
+        // We are in $CFG->dirroot.
+        $command = 'php admin/tool/generator/cli/maketestcourse.php';
+
+        $options = array(
+            '--shortname="' . $shortname . '"',
+            '--size="' . get_string('shortsize_' . $coursesize, 'tool_generator') . '"'
+        );
+
+        if (!$this->progress) {
+            $options[] = '--quiet';
+        }
+
+        // Extend options.
+        $optionstoextend = array(
+            'fixeddataset' => 'fixeddataset',
+            'bypasscheck' => 'bypasscheck',
+        );
+
+        // Getting an options string.
+        foreach ($optionstoextend as $attribute => $option) {
+            if (!empty($this->{$attribute})) {
+                $options[] = '--' . $option;
+            }
+        }
+        $options = implode(' ', $options);
+        if ($this->progress) {
+            system($command . ' ' . $options, $exitcode);
+        } else {
+            passthru($command . ' ' . $options, $exitcode);
+        }
+
+        if ($exitcode != 0) {
+            exit($exitcode);
+        }
+    }
+
+    /**
+     * Obtains the last unique sufix (numeric) using the test course prefix.
+     *
+     * @return int The last generated numeric value.
+     */
+    protected function get_last_testcourse_id() {
+        global $DB;
+
+        $params = array();
+        $params['shortnameprefix'] = $DB->sql_like_escape(self::SHORTNAMEPREFIX) . '%';
+        $like = $DB->sql_like('shortname', ':shortnameprefix');
+
+        if (!$testcourses = $DB->get_records_select('course', $like, $params, 'shortname DESC')) {
+            return 0;
+        }
+
+        // They come ordered by shortname DESC, so non-numeric values will be the first ones.
+        foreach ($testcourses as $testcourse) {
+            $sufix = substr($testcourse->shortname, strlen(self::SHORTNAMEPREFIX));
+            if (is_numeric($sufix)) {
+                return $sufix;
+            }
+        }
+
+        // If all sufixes are not numeric this is the fist make test site run.
+        return 0;
+    }
+
+}
index b646e2f..407a58f 100644 (file)
@@ -34,6 +34,7 @@ list($options, $unrecognized) = cli_get_params(
         'help' => false,
         'shortname' => false,
         'size' => false,
+        'fixeddataset' => false,
         'bypasscheck' => false,
         'quiet' => false
     ),
@@ -53,6 +54,7 @@ level.
 Options:
 --shortname    Shortname of course to create (required)
 --size         Size of course to create XS, S, M, L, XL, or XXL (required)
+--fixeddataset Use a fixed data set instead of randomly generated data
 --bypasscheck  Bypasses the developer-mode check (be careful!)
 --quiet        Do not show any output
 
@@ -73,16 +75,17 @@ if (empty($options['bypasscheck']) && !debugging('', DEBUG_DEVELOPER)) {
 // Get options.
 $shortname = $options['shortname'];
 $sizename = $options['size'];
+$fixeddataset = $options['fixeddataset'];
 
 // Check size.
 try {
-    $size = tool_generator_backend::size_for_name($sizename);
+    $size = tool_generator_course_backend::size_for_name($sizename);
 } catch (coding_exception $e) {
     cli_error("Invalid size ($sizename). Use --help for help.");
 }
 
 // Check shortname.
-if ($error = tool_generator_backend::check_shortname_available($shortname)) {
+if ($error = tool_generator_course_backend::check_shortname_available($shortname)) {
     cli_error($error);
 }
 
@@ -90,5 +93,5 @@ if ($error = tool_generator_backend::check_shortname_available($shortname)) {
 session_set_user(get_admin());
 
 // Do backend code to generate course.
-$backend = new tool_generator_backend($shortname, $size, empty($options['quiet']));
+$backend = new tool_generator_course_backend($shortname, $size, $fixeddataset, empty($options['quiet']));
 $id = $backend->make();
diff --git a/admin/tool/generator/cli/maketestsite.php b/admin/tool/generator/cli/maketestsite.php
new file mode 100644 (file)
index 0000000..bc91d7d
--- /dev/null
@@ -0,0 +1,95 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * CLI interface for creating a test site.
+ *
+ * @package tool_generator
+ * @copyright 2013 David Monllaó
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+define('CLI_SCRIPT', true);
+define('NO_OUTPUT_BUFFERING', true);
+
+require(__DIR__ . '/../../../../config.php');
+require_once($CFG->libdir. '/clilib.php');
+
+// CLI options.
+list($options, $unrecognized) = cli_get_params(
+    array(
+        'help' => false,
+        'size' => false,
+        'fixeddataset' => false,
+        'bypasscheck' => false,
+        'quiet' => false
+    ),
+    array(
+        'h' => 'help'
+    )
+);
+
+$sitesizes = '* ' . implode(PHP_EOL . '* ', tool_generator_site_backend::get_size_choices());
+
+// Display help.
+if (!empty($options['help']) || empty($options['size'])) {
+    echo "
+Utility to generate a standard test site data set.
+
+Not for use on live sites; only normally works if debugging is set to DEVELOPER
+level.
+
+Consider that, depending on the size you select, this CLI tool can really generate a lot of data, aproximated sizes:
+
+$sitesizes
+
+Options:
+--size         Size of the generated site, this value affects the number of courses and their size. Accepted values: XS, S, M, L, XL, or XXL (required)
+--fixeddataset Use a fixed data set instead of randomly generated data
+--bypasscheck  Bypasses the developer-mode check (be careful!)
+--quiet        Do not show any output
+
+-h, --help     Print out this help
+
+Example from Moodle root directory:
+\$ php admin/tool/generator/cli/maketestsite.php --size=S
+";
+    // Exit with error unless we're showing this because they asked for it.
+    exit(empty($options['help']) ? 1 : 0);
+}
+
+// Check debugging is set to developer level.
+if (empty($options['bypasscheck']) && !$CFG->debugdeveloper) {
+    cli_error(get_string('error_notdebugging', 'tool_generator'));
+}
+
+// Get options.
+$sizename = $options['size'];
+$fixeddataset = $options['fixeddataset'];
+
+// Check size.
+try {
+    $size = tool_generator_site_backend::size_for_name($sizename);
+} catch (coding_exception $e) {
+    cli_error("Invalid size ($sizename). Use --help for help.");
+}
+
+// Switch to admin user account.
+session_set_user(get_admin());
+
+// Do backend code to generate site.
+$backend = new tool_generator_site_backend($size, $options['bypasscheck'], $fixeddataset, empty($options['quiet']));
+$backend->make();
index 103a4fd..a204ce8 100644 (file)
  */
 
 $string['bigfile'] = 'Big file {$a}';
+$string['coursesize_0'] = 'XS (~10KB; create in ~1 second)';
+$string['coursesize_1'] = 'S (~10MB; create in ~30 seconds)';
+$string['coursesize_2'] = 'M (~100MB; create in ~5 minutes)';
+$string['coursesize_3'] = 'L (~1GB; create in ~1 hour)';
+$string['coursesize_4'] = 'XL (~10GB; create in ~4 hours)';
+$string['coursesize_5'] = 'XXL (~20GB; create in ~8 hours)';
 $string['createcourse'] = 'Create course';
 $string['creating'] = 'Creating course';
 $string['done'] = 'done ({$a}s)';
@@ -48,27 +54,28 @@ $string['error_notdebugging'] = 'Not available on this server because debugging
 $string['firstname'] = 'Test course user';
 $string['fullname'] = 'Test course: {$a->size}';
 $string['maketestcourse'] = 'Make test course';
-$string['pluginname'] = 'Random course generator';
+$string['pluginname'] = 'Development data generator';
 $string['progress_createcourse'] = 'Creating course {$a}';
 $string['progress_checkaccounts'] = 'Checking user accounts ({$a})';
+$string['progress_coursecompleted'] = 'Course completed ({$a}s)';
 $string['progress_createaccounts'] = 'Creating user accounts ({$a->from} - {$a->to})';
 $string['progress_createbigfiles'] = 'Creating big files ({$a})';
 $string['progress_createforum'] = 'Creating forum ({$a} posts)';
 $string['progress_createpages'] = 'Creating pages ({$a})';
 $string['progress_createsmallfiles'] = 'Creating small files ({$a})';
 $string['progress_enrol'] = 'Enrolling users into course ({$a})';
-$string['progress_complete'] = 'Complete ({$a}s)';
+$string['progress_sitecompleted'] = 'Site completed ({$a}s)';
 $string['shortsize_0'] = 'XS';
 $string['shortsize_1'] = 'S';
 $string['shortsize_2'] = 'M';
 $string['shortsize_3'] = 'L';
 $string['shortsize_4'] = 'XL';
 $string['shortsize_5'] = 'XXL';
+$string['sitesize_0'] = 'XS (~10MB; 3 courses, created in ~30 seconds)';
+$string['sitesize_1'] = 'S (~50MB; 8 courses, created in ~2 minutes)';
+$string['sitesize_2'] = 'M (~200MB; 73 courses, created in ~10 minutes)';
+$string['sitesize_3'] = 'L (~1\'5GB; 277 courses, created in ~1\'5 hours)';
+$string['sitesize_4'] = 'XL (~10GB; 1065 courses, created in ~5 hours)';
+$string['sitesize_5'] = 'XXL (~20GB; 4177 courses, created in ~10 hours)';
 $string['size'] = 'Size of course';
-$string['size_0'] = 'XS (~10KB; create in ~1 second)';
-$string['size_1'] = 'S (~10MB; create in ~30 seconds)';
-$string['size_2'] = 'M (~100MB; create in ~5 minutes)';
-$string['size_3'] = 'L (~1GB; create in ~1 hour)';
-$string['size_4'] = 'XL (~10GB; create in ~4 hours)';
-$string['size_5'] = 'XXL (~20GB; create in ~8 hours)';
 $string['smallfiles'] = 'Small files';
index f3ff9f7..82af097 100644 (file)
@@ -55,7 +55,7 @@ $mform = new tool_generator_make_form('maketestcourse.php');
 if ($data = $mform->get_data()) {
     // Do actual work.
     echo $OUTPUT->heading(get_string('creating', 'tool_generator'));
-    $backend = new tool_generator_backend($data->shortname, $data->size);
+    $backend = new tool_generator_course_backend($data->shortname, $data->size);
     $id = $backend->make();
 
     echo html_writer::div(
index 3b2244d..b8637f4 100644 (file)
@@ -35,7 +35,7 @@ class tool_generator_maketestcourse_testcase extends advanced_testcase {
         $this->setAdminUser();
 
         // Create the XS course.
-        $backend = new tool_generator_backend('TOOL_MAKELARGECOURSE_XS', 0, false);
+        $backend = new tool_generator_course_backend('TOOL_MAKELARGECOURSE_XS', 0, false, false);
         $courseid = $backend->make();
 
         // Get course details.
@@ -107,4 +107,48 @@ class tool_generator_maketestcourse_testcase extends advanced_testcase {
                     fd.forum = ?", array($forum->instance));
         $this->assertEquals(2, $posts);
     }
+
+    /**
+     * Creates an small test course with fixed data set and checks the used sections and users.
+     */
+    public function test_fixed_data_set() {
+        global $DB;
+
+        $this->resetAfterTest();
+        $this->setAdminUser();
+
+        // Create the S course (more sections and activities than XS).
+        $backend = new tool_generator_course_backend('TOOL_S_COURSE_1', 1, true, false);
+        $courseid = $backend->make();
+
+        // Get course details.
+        $course = get_course($courseid);
+        $modinfo = get_fast_modinfo($course);
+
+        // Check module instances belongs to section 1.
+        $instances = $modinfo->get_instances_of('page');
+        $npageinstances = count($instances);
+        foreach ($instances as $instance) {
+            $this->assertEquals(1, $instance->sectionnum);
+        }
+
+        // Users that started discussions are the same.
+        $forums = $modinfo->get_instances_of('forum');
+        $nforuminstances = count($forums);
+        $discussions = forum_get_discussions(reset($forums), 'd.timemodified ASC');
+        $lastusernumber = 0;
+        $discussionstarters = array();
+        foreach ($discussions as $discussion) {
+            $usernumber = intval($discussion->lastname);
+
+            // Checks that the users are odd numbers.
+            $this->assertEquals(1, $usernumber % 2);
+
+            // Checks that the users follows an increasing order.
+            $this->assertGreaterThan($lastusernumber, $usernumber);
+            $lastusernumber = $usernumber;
+            $discussionstarters[$discussion->userid] = $discussion->subject;
+        }
+
+    }
 }
index 66aa662..3733d98 100644 (file)
@@ -24,6 +24,6 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$plugin->version = 2013080700;
-$plugin->requires = 2013080200;
+$plugin->version = 2013090200;
+$plugin->requires = 2013090200;
 $plugin->component = 'tool_generator';
index c052d1c..7949d94 100644 (file)
@@ -27,6 +27,7 @@ require('../../../config.php');
 require_once($CFG->libdir.'/adminlib.php');
 require_once($CFG->libdir.'/csvlib.class.php');
 require_once($CFG->dirroot.'/user/profile/lib.php');
+require_once($CFG->dirroot.'/user/lib.php');
 require_once($CFG->dirroot.'/group/lib.php');
 require_once($CFG->dirroot.'/cohort/lib.php');
 require_once('locallib.php');
@@ -654,9 +655,8 @@ if ($formdata = $mform2->is_cancelled()) {
             }
 
             if ($doupdate or $existinguser->password !== $oldpw) {
-                // we want only users that were really updated
-
-                $DB->update_record('user', $existinguser);
+                // We want only users that were really updated.
+                user_update_user($existinguser, false);
 
                 $upt->track('status', $struserupdated);
                 $usersupdated++;
@@ -668,8 +668,6 @@ if ($formdata = $mform2->is_cancelled()) {
                     profile_save_data($existinguser);
                 }
 
-                events_trigger('user_updated', $existinguser);
-
                 if ($bulk == UU_BULK_UPDATED or $bulk == UU_BULK_ALL) {
                     if (!in_array($user->id, $SESSION->bulk_users)) {
                         $SESSION->bulk_users[] = $user->id;
@@ -789,8 +787,7 @@ if ($formdata = $mform2->is_cancelled()) {
                 $upt->track('password', '-', 'normal', false);
             }
 
-            // create user - insert_record ignores any extra properties
-            $user->id = $DB->insert_record('user', $user);
+            $user->id = user_create_user($user, false);
             $upt->track('username', html_writer::link(new moodle_url('/user/profile.php', array('id'=>$user->id)), s($user->username)), 'normal', false);
 
             // pre-process custom profile menu fields data from csv file
@@ -812,8 +809,6 @@ if ($formdata = $mform2->is_cancelled()) {
             // make sure user context exists
             context_user::instance($user->id);
 
-            events_trigger('user_created', $user);
-
             if ($bulk == UU_BULK_NEW or $bulk == UU_BULK_ALL) {
                 if (!in_array($user->id, $SESSION->bulk_users)) {
                     $SESSION->bulk_users[] = $user->id;
index 0f66603..699babb 100644 (file)
@@ -4,6 +4,7 @@
     require_once($CFG->libdir.'/adminlib.php');
     require_once($CFG->libdir.'/authlib.php');
     require_once($CFG->dirroot.'/user/filters/lib.php');
+    require_once($CFG->dirroot.'/user/lib.php');
 
     $delete       = optional_param('delete', 0, PARAM_INT);
     $confirm      = optional_param('confirm', '', PARAM_ALPHANUM);   //md5 confirmation hash
         if ($user = $DB->get_record('user', array('id'=>$suspend, 'mnethostid'=>$CFG->mnet_localhost_id, 'deleted'=>0))) {
             if (!is_siteadmin($user) and $USER->id != $user->id and $user->suspended != 1) {
                 $user->suspended = 1;
-                $user->timemodified = time();
-                $DB->set_field('user', 'suspended', $user->suspended, array('id'=>$user->id));
-                $DB->set_field('user', 'timemodified', $user->timemodified, array('id'=>$user->id));
-                // force logout
+                // Force logout.
                 session_kill_user($user->id);
-                events_trigger('user_updated', $user);
+                user_update_user($user, false);
             }
         }
         redirect($returnurl);
         if ($user = $DB->get_record('user', array('id'=>$unsuspend, 'mnethostid'=>$CFG->mnet_localhost_id, 'deleted'=>0))) {
             if ($user->suspended != 0) {
                 $user->suspended = 0;
-                $user->timemodified = time();
-                $DB->set_field('user', 'suspended', $user->suspended, array('id'=>$user->id));
-                $DB->set_field('user', 'timemodified', $user->timemodified, array('id'=>$user->id));
-                events_trigger('user_updated', $user);
+                user_update_user($user, false);
             }
         }
         redirect($returnurl);
index de5061d..a01fc6e 100644 (file)
@@ -284,6 +284,7 @@ class auth_plugin_db extends auth_plugin_base {
             $remove_users = $DB->get_records_sql($sql, $params);
 
             if (!empty($remove_users)) {
+                require_once($CFG->dirroot.'/user/lib.php');
                 $trace->output(get_string('auth_dbuserstoremove','auth_db', count($remove_users)));
 
                 foreach ($remove_users as $user) {
@@ -294,8 +295,7 @@ class auth_plugin_db extends auth_plugin_base {
                         $updateuser = new stdClass();
                         $updateuser->id   = $user->id;
                         $updateuser->suspended = 1;
-                        $updateuser->timemodified = time();
-                        $DB->update_record('user', $updateuser);
+                        user_update_user($updateuser, false);
                         $trace->output(get_string('auth_dbsuspenduser', 'auth_db', array('name'=>$user->username, 'id'=>$user->id)), 1);
                     }
                 }
index e459004..a670cfc 100644 (file)
@@ -87,17 +87,15 @@ class auth_plugin_email extends auth_plugin_base {
     function user_signup($user, $notify=true) {
         global $CFG, $DB;
         require_once($CFG->dirroot.'/user/profile/lib.php');
+        require_once($CFG->dirroot.'/user/lib.php');
 
         $user->password = hash_internal_user_password($user->password);
 
-        $user->id = $DB->insert_record('user', $user);
+        $user->id = user_create_user($user, false);
 
-        /// Save any custom profile field information
+        // Save any custom profile field information.
         profile_save_data($user);
 
-        $user = $DB->get_record('user', array('id'=>$user->id));
-        events_trigger('user_created', $user);
-
         if (! send_confirmation_email($user)) {
             print_error('auth_emailnoemail','auth_email');
         }
index a1a9645..1bad153 100644 (file)
@@ -78,6 +78,7 @@ if (!defined('LDAP_OPT_DIAGNOSTIC_MESSAGE')) {
 
 require_once($CFG->libdir.'/authlib.php');
 require_once($CFG->libdir.'/ldaplib.php');
+require_once($CFG->dirroot.'/user/lib.php');
 
 /**
  * LDAP authentication plugin.
@@ -550,7 +551,7 @@ class auth_plugin_ldap extends auth_plugin_base {
             print_error('auth_ldap_create_error', 'auth_ldap');
         }
 
-        $user->id = $DB->insert_record('user', $user);
+        $user->id = user_create_user($user, false);
 
         // Save any custom profile field information
         profile_save_data($user);
@@ -562,7 +563,6 @@ class auth_plugin_ldap extends auth_plugin_base {
         update_internal_user_password($user, $plainslashedpassword);
 
         $user = $DB->get_record('user', array('id'=>$user->id));
-        events_trigger('user_created', $user);
 
         if (! send_confirmation_email($user)) {
             print_error('noemail', 'auth_ldap');
@@ -612,12 +612,12 @@ class auth_plugin_ldap extends auth_plugin_base {
                 if (!$this->user_activate($username)) {
                     return AUTH_CONFIRM_FAIL;
                 }
-                $DB->set_field('user', 'confirmed', 1, array('id'=>$user->id));
+                $user->confirmed = 1;
                 if ($user->firstaccess == 0) {
-                    $DB->set_field('user', 'firstaccess', time(), array('id'=>$user->id));
+                    $user->firstaccess = time();
                 }
-                $euser = $DB->get_record('user', array('id' => $user->id));
-                events_trigger('user_updated', $euser);
+                require_once($CFG->dirroot.'/user/lib.php');
+                user_update_user($user, false);
                 return AUTH_CONFIRM_OK;
             }
         } else {
@@ -806,10 +806,8 @@ class auth_plugin_ldap extends auth_plugin_base {
                     $updateuser = new stdClass();
                     $updateuser->id = $user->id;
                     $updateuser->suspended = 1;
-                    $DB->update_record('user', $updateuser);
+                    user_update_user($updateuser, false);
                     echo "\t"; print_string('auth_dbsuspenduser', 'auth_db', array('name'=>$user->username, 'id'=>$user->id)); echo "\n";
-                    $euser = $DB->get_record('user', array('id' => $user->id));
-                    events_trigger('user_updated', $euser);
                     session_kill_user($user->id);
                 }
             } else {
@@ -835,10 +833,8 @@ class auth_plugin_ldap extends auth_plugin_base {
                     $updateuser->id = $user->id;
                     $updateuser->auth = $this->authtype;
                     $updateuser->suspended = 0;
-                    $DB->update_record('user', $updateuser);
+                    user_update_user($updateuser, false);
                     echo "\t"; print_string('auth_dbreviveduser', 'auth_db', array('name'=>$user->username, 'id'=>$user->id)); echo "\n";
-                    $euser = $DB->get_record('user', array('id' => $user->id));
-                    events_trigger('user_updated', $euser);
                 }
             } else {
                 print_string('nouserentriestorevive', 'auth_ldap');
@@ -950,10 +946,10 @@ class auth_plugin_ldap extends auth_plugin_base {
                     $user->lang = $CFG->lang;
                 }
 
-                $id = $DB->insert_record('user', $user);
+                $id = user_create_user($user, false);
                 echo "\t"; print_string('auth_dbinsertuser', 'auth_db', array('name'=>$user->username, 'id'=>$id)); echo "\n";
                 $euser = $DB->get_record('user', array('id' => $id));
-                events_trigger('user_created', $euser);
+
                 if (!empty($this->config->forcechangepassword)) {
                     set_user_preference('auth_forcepasswordchange', 1, $id);
                 }
@@ -1011,22 +1007,25 @@ class auth_plugin_ldap extends auth_plugin_base {
                 $updatekeys = array_keys($newinfo);
             }
 
-            foreach ($updatekeys as $key) {
-                if (isset($newinfo[$key])) {
-                    $value = $newinfo[$key];
-                } else {
-                    $value = '';
-                }
+            if (!empty($updatekeys)) {
+                $newuser = new stdClass();
+                $newuser->id = $userid;
 
-                if (!empty($this->config->{'field_updatelocal_' . $key})) {
-                    if ($user->{$key} != $value) { // only update if it's changed
-                        $DB->set_field('user', $key, $value, array('id'=>$userid));
+                foreach ($updatekeys as $key) {
+                    if (isset($newinfo[$key])) {
+                        $value = $newinfo[$key];
+                    } else {
+                        $value = '';
+                    }
+
+                    if (!empty($this->config->{'field_updatelocal_' . $key})) {
+                        // Only update if it's changed.
+                        if ($user->{$key} != $value) {
+                            $newuser->$key = $value;
+                        }
                     }
                 }
-            }
-            if (!empty($updatekeys)) {
-                $euser = $DB->get_record('user', array('id' => $userid));
-                events_trigger('user_updated', $euser);
+                user_update_user($newuser, false);
             }
         } else {
             return false;
@@ -1646,8 +1645,7 @@ class auth_plugin_ldap extends auth_plugin_base {
             // Now start the whole NTLM machinery.
             if($this->config->ntlmsso_ie_fastpath == AUTH_NTLM_FASTPATH_YESATTEMPT ||
                 $this->config->ntlmsso_ie_fastpath == AUTH_NTLM_FASTPATH_YESFORM) {
-
-                if(check_browser_version('MSIE')) {
+                if (core_useragent::check_ie_version()) {
                     $sesskey = sesskey();
                     redirect($CFG->wwwroot.'/auth/ldap/ntlmsso_magic.php?sesskey='.$sesskey);
                 } else if ($this->config->ntlmsso_ie_fastpath == AUTH_NTLM_FASTPATH_YESFORM) {
index 0d349c5..2ac49a3 100644 (file)
@@ -28,7 +28,7 @@ $file = $CFG->dirroot.'/pix/spacer.gif';
 
 if ($authplugin->ntlmsso_magic($sesskey) && file_exists($file)) {
     if (!empty($authplugin->config->ntlmsso_ie_fastpath)) {
-        if (check_browser_version('MSIE')) {
+        if (core_useragent::check_ie_version()) {
             // $PAGE->https_required() up above takes care of what $CFG->httpswwwroot should be.
             redirect($CFG->httpswwwroot.'/auth/ldap/ntlmsso_finish.php');
         }
index c1861fa..2f626bd 100644 (file)
@@ -216,6 +216,7 @@ class auth_plugin_mnet extends auth_plugin_base {
         global $CFG, $DB;
         require_once $CFG->dirroot . '/mnet/xmlrpc/client.php';
         require_once $CFG->libdir . '/gdlib.php';
+        require_once($CFG->dirroot.'/user/lib.php');
 
         // verify the remote host is configured locally before attempting RPC call
         if (! $remotehost = $DB->get_record('mnet_host', array('wwwroot' => $remotepeer->wwwroot, 'deleted' => 0))) {
@@ -361,8 +362,7 @@ class auth_plugin_mnet extends auth_plugin_base {
         if (empty($localuser->firstaccess)) { // Now firstaccess, grab it here
             $localuser->firstaccess = time();
         }
-
-        $DB->update_record('user', $localuser);
+        user_update_user($localuser, false);
 
         if (!$firsttime) {
             // repeat customer! let the IDP know about enrolments
index 2b6cdab..444f9cf 100644 (file)
@@ -1421,6 +1421,12 @@ class restore_course_structure_step extends restore_structure_step {
             $data->theme = '';
         }
 
+        // Check if this is an old SCORM course format.
+        if ($data->format == 'scorm') {
+            $data->format = 'singleactivity';
+            $data->activitytype = 'scorm';
+        }
+
         // Course record ready, update it
         $DB->update_record('course', $data);
 
@@ -3624,7 +3630,7 @@ class restore_process_file_aliases_queue extends restore_execution_step {
 
         // Iterate over aliases in the queue.
         foreach ($rs as $record) {
-            $info = restore_dbops::decode_backup_temp_info($record->info);
+            $info = backup_controller_dbops::decode_backup_temp_info($record->info);
 
             // Try to pick a repository instance that should serve the alias.
             $repository = $this->choose_repository($info);
index dcfddec..5277416 100644 (file)
@@ -59,7 +59,7 @@ class database_logger extends base_logger {
         if ($this->levelcol) {
             $columns[$this->levelcol] = $level;
         }
-        $columns[$this->messagecol] = $message; //TODO: should this be cleaned?
+        $columns[$this->messagecol] = clean_param($message, PARAM_NOTAGS);
         return $this->insert_log_record($this->logtable, $columns);
     }
 
@@ -69,6 +69,6 @@ class database_logger extends base_logger {
         // to preserve DB logs if the whole backup/restore transaction is
         // rollback
         global $DB;
-        return $DB->insert_record($this->logtable, $columns, false); // Don't return inserted id
+        return $DB->insert_record($table, $columns, false); // Don't return inserted id
     }
 }
index 7d70be9..773a156 100644 (file)
@@ -69,7 +69,7 @@ class backup_base_optigroup_testcase extends basic_testcase {
     /**
      * Incorrect creation tests (attributes and final elements)
      */
-    function itest_wrong_creation() {
+    function test_wrong_creation() {
 
         // Create instance with invalid name
         try {
index b9a2277..f89837c 100644 (file)
@@ -140,7 +140,7 @@ class core_backup_renderer extends plugin_renderer_base {
                     }
                     if (empty($table)) {
                         $table = new html_table();
-                        $table->head = array('Module', 'Title', 'Userinfo');
+                        $table->head = array(get_string('module','backup'), get_string('title','backup'), get_string('userinfo','backup'));
                         $table->colclasses = array('modulename', 'moduletitle', 'userinfoincluded');
                         $table->align = array('left','left', 'center');
                         $table->attributes = array('class'=>'activitytable generaltable');
index f415f07..f046af1 100644 (file)
@@ -100,13 +100,6 @@ Feature: Restore Moodle 2 course backups
     And I follow "Edit settings"
     And I expand all fieldsets
     And the "id_format" field should match "Social format" value
-    And I fill the moodle form with:
-      | id_format | SCORM format |
-    And I press "Save changes"
-    And I should see "Adding a new SCORM package"
-    And I follow "Edit settings"
-    And I expand all fieldsets
-    And the "id_format" field should match "SCORM format" value
     And I press "Cancel"
 
   @javascript
index 6d7b054..39c2c4c 100644 (file)
@@ -72,7 +72,7 @@ class core_badges_observer {
     /**
      * Triggered when 'course_completed' event is triggered.
      *
-     * @param   \core\event\course_completed $event
+     * @param \core\event\course_completed $event
      */
     public static function course_criteria_review(\core\event\course_completed $event) {
         global $DB, $CFG;
@@ -105,4 +105,36 @@ class core_badges_observer {
             }
         }
     }
+
+    /**
+     * Triggered when 'user_updated' event happens.
+     *
+     * @param \core\event\user_updated $event event generated when user profile is updated.
+     */
+    public static function profile_criteria_review(\core\event\user_updated $event) {
+        global $DB, $CFG;
+
+        if (!empty($CFG->enablebadges)) {
+            require_once($CFG->dirroot.'/lib/badgeslib.php');
+            $userid = $event->objectid;
+
+            if ($rs = $DB->get_records('badge_criteria', array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE))) {
+                foreach ($rs as $r) {
+                    $badge = new badge($r->badgeid);
+                    if (!$badge->is_active() || $badge->is_issued($userid)) {
+                        continue;
+                    }
+
+                    if ($badge->criteria[BADGE_CRITERIA_TYPE_PROFILE]->review($userid)) {
+                        $badge->criteria[BADGE_CRITERIA_TYPE_PROFILE]->mark_complete($userid);
+
+                        if ($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->review($userid)) {
+                            $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->mark_complete($userid);
+                            $badge->issue($userid);
+                        }
+                    }
+                }
+            }
+        }
+    }
 }
index 5436f01..e9c4ab3 100644 (file)
 require_once(dirname(dirname(__FILE__)) . '/config.php');
 require_once($CFG->libdir . '/badgeslib.php');
 
-$json = required_param('badge', PARAM_RAW);
+$json = optional_param('badge', null, PARAM_RAW);
+// Redirect to homepage if users are trying to access external badge through old url.
+if ($json) {
+    redirect($CFG->wwwroot, get_string('invalidrequest', 'error'), 3);
+}
+
+$hash = required_param('hash', PARAM_ALPHANUM);
+$userid = required_param('user', PARAM_INT);
+
+$PAGE->set_url(new moodle_url('/badges/external.php', array('hash' => $hash, 'user' => $userid)));
+
+// Using the same setting as user profile page.
+if (!empty($CFG->forceloginforprofiles)) {
+    require_login();
+    if (isguestuser()) {
+        $SESSION->wantsurl = $PAGE->url->out(false);
+        redirect(get_login_url());
+    }
+} else if (!empty($CFG->forcelogin)) {
+    require_login();
+}
+
+// Get all external badges of a user.
+$out = get_backpack_settings($userid);
+$badges = $out->badges;
+
+// Loop through the badges and check if supplied badge hash exists in user external badges.
+foreach ($badges as $b) {
+    if ($hash == hash("md5", $b->hostedUrl)) {
+        $badge = $b;
+        break;
+    }
+}
+
+// If we didn't find the badge, a user might be trying to replace userid parameter.
+if (is_null($badge)) {
+    print_error(get_string('error:externalbadgedoesntexist', 'badges'));
+}
 
 $PAGE->set_context(context_system::instance());
 $output = $PAGE->get_renderer('core', 'badges');
 
-$badge = new external_badge(unserialize($json));
+$badge = new external_badge($badge, $userid);
 
-$PAGE->set_url('/badges/external.php');
 $PAGE->set_pagelayout('base');
 $PAGE->set_title(get_string('issuedbadge', 'badges'));
 
index 1f52453..875d025 100644 (file)
@@ -53,11 +53,13 @@ $PAGE->set_heading($title);
 $PAGE->set_pagelayout('mydashboard');
 
 $backpack = $DB->get_record('badge_backpack', array('userid' => $USER->id));
+$badgescache = cache::make('core', 'externalbadges');
 
 if ($disconnect && $backpack) {
     require_sesskey();
     $DB->delete_records('badge_external', array('backpackid' => $backpack->id));
     $DB->delete_records('badge_backpack', array('userid' => $USER->id));
+    $badgescache->delete($USER->id);
     redirect(new moodle_url('/badges/mybackpack.php'));
 }
 
@@ -103,6 +105,7 @@ if ($backpack) {
                 $DB->insert_record('badge_external', $obj);
             }
         }
+        $badgescache->delete($USER->id);
         redirect(new moodle_url('/badges/mybadges.php'));
     }
 } else {
index 088b443..3075dc1 100644 (file)
@@ -81,9 +81,10 @@ class core_badges_renderer extends plugin_renderer_base {
                 $url = new moodle_url('badge.php', array('hash' => $badge->uniquehash));
             } else {
                 if (!$external) {
-                    $url = new moodle_url($CFG->wwwroot . '/badges/badge.php', array('hash' => $badge->uniquehash));
+                    $url = new moodle_url('/badges/badge.php', array('hash' => $badge->uniquehash));
                 } else {
-                    $url = new moodle_url($CFG->wwwroot . '/badges/external.php', array('badge' => serialize($badge)));
+                    $hash = hash('md5', $badge->hostedUrl);
+                    $url = new moodle_url('/badges/external.php', array('hash' => $hash, 'user' => $userid));
                 }
             }
             $actions = html_writer::tag('div', $push . $download . $status, array('class' => 'badge-actions'));
@@ -276,6 +277,7 @@ class core_badges_renderer extends plugin_renderer_base {
     protected function render_issued_badge(issued_badge $ibadge) {
         global $USER, $CFG, $DB;
         $issued = $ibadge->issued;
+        $userinfo = $ibadge->recipient;
         $badge = new badge($ibadge->badgeid);
         $today_date = date('Y-m-d');
         $today = strtotime($today_date);
@@ -286,7 +288,7 @@ class core_badges_renderer extends plugin_renderer_base {
         $imagetable = new html_table();
         $imagetable->attributes = array('class' => 'clearfix badgeissuedimage');
         $imagetable->data[] = array(html_writer::empty_tag('img', array('src' => $issued['badge']['image'])));
-        if ($USER->id == $ibadge->recipient && !empty($CFG->enablebadges)) {
+        if ($USER->id == $userinfo->id && !empty($CFG->enablebadges)) {
             $imagetable->data[] = array($this->output->single_button(
                         new moodle_url('/badges/badge.php', array('hash' => $ibadge->hash, 'bake' => true)),
                         get_string('download'),
@@ -307,11 +309,20 @@ class core_badges_renderer extends plugin_renderer_base {
         $datatable = new html_table();
         $datatable->attributes = array('class' => 'badgeissuedinfo');
         $datatable->colclasses = array('bfield', 'bvalue');
+
+        // Recipient information.
+        $datatable->data[] = array($this->output->heading(get_string('recipientdetails', 'badges'), 3), '');
+        $datatable->data[] = array(get_string('name'), fullname($userinfo));
+        if (empty($userinfo->backpackemail)) {
+            $datatable->data[] = array(get_string('email'), obfuscate_mailto($userinfo->accountemail));
+        } else {
+            $datatable->data[] = array(get_string('email'), obfuscate_mailto($userinfo->backpackemail));
+        }
+
         $datatable->data[] = array($this->output->heading(get_string('issuerdetails', 'badges'), 3), '');
         $datatable->data[] = array(get_string('issuername', 'badges'), $badge->issuername);
         if (isset($badge->issuercontact) && !empty($badge->issuercontact)) {
-            $datatable->data[] = array(get_string('contact', 'badges'),
-                html_writer::tag('a', $badge->issuercontact, array('href' => 'mailto:' . $badge->issuercontact)));
+            $datatable->data[] = array(get_string('contact', 'badges'), obfuscate_mailto($badge->issuercontact));
         }
         $datatable->data[] = array($this->output->heading(get_string('badgedetails', 'badges'), 3), '');
         $datatable->data[] = array(get_string('name'), $badge->name);
@@ -347,7 +358,7 @@ class core_badges_renderer extends plugin_renderer_base {
 
         // Print evidence.
         $agg = $badge->get_aggregation_methods();
-        $evidence = $badge->get_criteria_completions($ibadge->recipient);
+        $evidence = $badge->get_criteria_completions($userinfo->id);
         $eids = array_map(create_function('$o', 'return $o->critid;'), $evidence);
         unset($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]);
 
@@ -378,6 +389,7 @@ class core_badges_renderer extends plugin_renderer_base {
         $issued = $ibadge->issued;
         $assertion = $issued->assertion;
         $issuer = $assertion->badge->issuer;
+        $userinfo = $ibadge->recipient;
         $table = new html_table();
 
         $imagetable = new html_table();
@@ -387,13 +399,29 @@ class core_badges_renderer extends plugin_renderer_base {
         $datatable = new html_table();
         $datatable->attributes = array('class' => 'badgeissuedinfo');
         $datatable->colclasses = array('bfield', 'bvalue');
+
+        // Recipient information.
+        $datatable->data[] = array($this->output->heading(get_string('recipientdetails', 'badges'), 3), '');
+        // Technically, we should alway have a user at this point, but added an extra check just in case.
+        if ($userinfo) {
+            $datatable->data[] = array(get_string('name'), fullname($userinfo));
+            if (!$ibadge->valid) {
+                $notify = $this->output->notification(get_string('recipientvalidationproblem', 'badges'), 'notifynotice');
+                $datatable->data[] = array(get_string('email'), obfuscate_mailto($userinfo->email) . $notify);
+            } else {
+                $datatable->data[] = array(get_string('email'), obfuscate_mailto($userinfo->email));
+            }
+        } else {
+            $notify = $this->output->notification(get_string('recipientidentificationproblem', 'badges'), 'notifynotice');
+            $datatable->data[] = array(get_string('name'), $notify);
+        }
+
         $datatable->data[] = array($this->output->heading(get_string('issuerdetails', 'badges'), 3), '');
         $datatable->data[] = array(get_string('issuername', 'badges'), $issuer->name);
         $datatable->data[] = array(get_string('issuerurl', 'badges'),
                 html_writer::tag('a', $issuer->origin, array('href' => $issuer->origin)));
         if (isset($issuer->contact)) {
-            $datatable->data[] = array(get_string('contact', 'badges'),
-                html_writer::tag('a', $issuer->contact, array('href' => 'mailto:' . $issuer->contact)));
+            $datatable->data[] = array(get_string('contact', 'badges'), obfuscate_mailto($issuer->contact));
         }
         $datatable->data[] = array($this->output->heading(get_string('badgedetails', 'badges'), 3), '');
         $datatable->data[] = array(get_string('name'), $assertion->badge->name);
@@ -875,7 +903,7 @@ class issued_badge implements renderable {
     public $issued;
 
     /** @var badge recipient */
-    public $recipient = 0;
+    public $recipient;
 
     /** @var badge visibility to others */
     public $visible = 0;
@@ -901,7 +929,12 @@ class issued_badge implements renderable {
                 WHERE ' . $DB->sql_compare_text('uniquehash', 40) . ' = ' . $DB->sql_compare_text(':hash', 40),
                 array('hash' => $hash), IGNORE_MISSING);
         if ($rec) {
-            $this->recipient = $rec->userid;
+            // Get a recipient from database.
+            $user = $DB->get_record_sql('SELECT u.id, u.lastname, u.firstname,
+                                                u.email AS accountemail, b.email AS backpackemail
+                        FROM {user} u LEFT JOIN {badge_backpack} b ON u.id = b.userid
+                        WHERE u.id = :userid', array('userid' => $rec->userid));
+            $this->recipient = $user;
             $this->visible = $rec->visible;
             $this->badgeid = $rec->badgeid;
         }
@@ -915,13 +948,51 @@ class external_badge implements renderable {
     /** @var issued badge */
     public $issued;
 
+    /** @var User ID */
+    public $recipient;
+
+    /** @var validation of external badge */
+    public $valid = true;
+
     /**
      * Initializes the badge to display
      *
-     * @param string $json External badge information.
+     * @param object $badge External badge information.
+     * @param int $recipient User id.
      */
-    public function __construct($json) {
-        $this->issued = $json;
+    public function __construct($badge, $recipient) {
+        global $DB;
+        // At this point a user has connected a backpack. So, we are going to get
+        // their backpack email rather than their account email.
+        $user = $DB->get_record_sql('SELECT u.lastname, u.firstname, b.email
+                    FROM {user} u INNER JOIN {badge_backpack} b ON u.id = b.userid
+                    WHERE userid = :userid', array('userid' => $recipient), IGNORE_MISSING);
+
+        $this->issued = $badge;
+        $this->recipient = $user;
+
+        // Check if recipient is valid.
+        // There is no way to be 100% sure that a badge belongs to a user.
+        // Backpack does not return any recipient information.
+        // All we can do is compare that backpack email hashed using salt
+        // provided in the assertion matches a badge recipient from the assertion.
+        if ($user) {
+            if (validate_email($badge->assertion->recipient) && $badge->assertion->recipient == $user->email) {
+                // If we have email, compare emails.
+                $this->valid = true;
+            } else if ($badge->assertion->recipient == 'sha256$' . hash('sha256', $user->email)) {
+                // If recipient is hashed, but no salt, compare hashes without salt.
+                $this->valid = true;
+            } else if ($badge->assertion->recipient == 'sha256$' . hash('sha256', $user->email . $badge->assertion->salt)) {
+                // If recipient is hashed, compare hashes.
+                $this->valid = true;
+            } else {
+                // Otherwise, we cannot be sure that this user is a recipient.
+                $this->valid = false;
+            }
+        } else {
+            $this->valid = false;
+        }
     }
 }
 
@@ -1016,7 +1087,7 @@ class badge_user_collection extends badge_collection implements renderable {
         parent::__construct($badges);
 
         if (!empty($CFG->badges_allowexternalbackpack)) {
-            $this->backpack = get_backpack_settings($userid);
+            $this->backpack = get_backpack_settings($userid, true);
         }
     }
 }
index fa7b7cb..a3b2878 100644 (file)
@@ -260,4 +260,23 @@ class core_badgeslib_testcase extends advanced_testcase {
         $this->assertDebuggingCalled('Error baking badge image!');
         $this->assertTrue($badge->is_issued($this->user->id));
     }
+
+    /**
+     * Test badges observer when user_updated event is fired.
+     */
+    public function test_badges_observer_profile_criteria_review() {
+        $badge = new badge($this->coursebadge);
+        $this->assertFalse($badge->is_issued($this->user->id));
+
+        $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
+        $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
+        $criteria_overall1 = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $badge->id));
+        $criteria_overall1->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address'));
+
+        $this->user->address = 'Test address';
+        user_update_user($this->user, false);
+        // Check if badge is awarded.
+        $this->assertDebuggingCalled('Error baking badge image!');
+        $this->assertTrue($badge->is_issued($this->user->id));
+    }
 }
index d743f70..97da6fb 100644 (file)
@@ -595,8 +595,9 @@ class blog_listing {
             $userid = $USER->id;
         }
 
+        $allnamefields = get_all_user_name_fields(true, 'u');
         // The query used to locate blog entries is complicated.  It will be built from the following components:
-        $requiredfields = "p.*, u.firstname, u.lastname, u.email";  // the SELECT clause
+        $requiredfields = "p.*, $allnamefields, u.email";  // the SELECT clause
         $tables = array('p' => 'post', 'u' => 'user');   // components of the FROM clause (table_id => table_name)
         $conditions = array('u.deleted = 0', 'p.userid = u.id', '(p.module = \'blog\' OR p.module = \'blog_external\')');  // components of the WHERE clause (conjunction)
 
index da8fe67..6de94e2 100644 (file)
@@ -271,7 +271,7 @@ class cache implements cache_loader {
      *      In advanced cases an array may be useful such as in situations requiring the multi-key functionality.
      * @param int $strictness One of IGNORE_MISSING | MUST_EXIST
      * @return mixed|false The data from the cache or false if the key did not exist within the cache.
-     * @throws moodle_exception
+     * @throws coding_exception
      */
     public function get($key, $strictness = IGNORE_MISSING) {
         // 1. Parse the key.
@@ -329,7 +329,7 @@ class cache implements cache_loader {
         }
         // 5. Validate strictness.
         if ($strictness === MUST_EXIST && $result === false) {
-            throw new moodle_exception('Requested key did not exist in any cache stores and could not be loaded.');
+            throw new coding_exception('Requested key did not exist in any cache stores and could not be loaded.');
         }
         // 6. Set it to the store if we got it from the loader/datasource.
         if ($setaftervalidation) {
@@ -363,7 +363,7 @@ class cache implements cache_loader {
      * @return array An array of key value pairs for the items that could be retrieved from the cache.
      *      If MUST_EXIST was used and not all keys existed within the cache then an exception will be thrown.
      *      Otherwise any key that did not exist will have a data value of false within the results.
-     * @throws moodle_exception
+     * @throws coding_exception
      */
     public function get_many(array $keys, $strictness = IGNORE_MISSING) {
 
@@ -420,7 +420,7 @@ class cache implements cache_loader {
             $missingkeys = array();
             foreach ($result as $key => $value) {
                 if ($value === false) {
-                    $missingkeys[] = ($usingloader) ? $key : $parsedkeys[$key];
+                    $missingkeys[] = $parsedkeys[$key];
                 }
             }
             if (!empty($missingkeys)) {
@@ -430,11 +430,9 @@ class cache implements cache_loader {
                     $resultmissing = $this->datasource->load_many_for_cache($missingkeys);
                 }
                 foreach ($resultmissing as $key => $value) {
-                    $pkey = ($usingloader) ? $key : $keysparsed[$key];
-                    $realkey = ($usingloader) ? $parsedkeys[$key] : $key;
-                    $result[$pkey] = $value;
+                    $result[$keysparsed[$key]] = $value;
                     if ($value !== false) {
-                        $this->set($realkey, $value);
+                        $this->set($key, $value);
                     }
                 }
                 unset($resultmissing);
@@ -453,7 +451,7 @@ class cache implements cache_loader {
         if ($strictness === MUST_EXIST) {
             foreach ($keys as $key) {
                 if (!array_key_exists($key, $fullresult)) {
-                    throw new moodle_exception('Not all the requested keys existed within the cache stores.');
+                    throw new coding_exception('Not all the requested keys existed within the cache stores.');
                 }
             }
         }
@@ -483,6 +481,11 @@ class cache implements cache_loader {
         if ($this->perfdebug) {
             cache_helper::record_cache_set($this->storetype, $this->definition->get_id());
         }
+        if ($this->loader !== false) {
+            // We have a loader available set it there as well.
+            // We have to let the loader do its own parsing of data as it may be unique.
+            $this->loader->set($key, $data);
+        }
         if (is_object($data) && $data instanceof cacheable_object) {
             $data = new cache_cached_object($data);
         } else if (!is_scalar($data)) {
@@ -506,6 +509,7 @@ class cache implements cache_loader {
      * Removes references where required.
      *
      * @param stdClass|array $data
+     * @return mixed What ever was put in but without any references.
      */
     protected function unref($data) {
         if ($this->definition->uses_simple_data()) {
@@ -593,6 +597,11 @@ class cache implements cache_loader {
      *      ... if they care that is.
      */
     public function set_many(array $keyvaluearray) {
+        if ($this->loader !== false) {
+            // We have a loader available set it there as well.
+            // We have to let the loader do its own parsing of data as it may be unique.
+            $this->loader->set_many($keyvaluearray);
+        }
         $data = array();
         $simulatettl = $this->has_a_ttl() && !$this->store_supports_native_ttl();
         $usepersistcache = $this->is_using_persist_cache();
@@ -858,7 +867,7 @@ class cache implements cache_loader {
      * Returns the loader associated with this instance.
      *
      * @since 2.4.4
-     * @return cache_loader|false
+     * @return cache|false
      */
     protected function get_loader() {
         return $this->loader;
@@ -1340,7 +1349,6 @@ class cache_application extends cache implements cache_loader_with_locking {
      * @param string|int $key The key for the data being requested.
      * @param int $strictness One of IGNORE_MISSING | MUST_EXIST
      * @return mixed|false The data from the cache or false if the key did not exist within the cache.
-     * @throws moodle_exception
      */
     public function get($key, $strictness = IGNORE_MISSING) {
         if ($this->requirelockingread && $this->check_lock_state($key) === false) {
@@ -1364,7 +1372,7 @@ class cache_application extends cache implements cache_loader_with_locking {
      * @return array An array of key value pairs for the items that could be retrieved from the cache.
      *      If MUST_EXIST was used and not all keys existed within the cache then an exception will be thrown.
      *      Otherwise any key that did not exist will have a data value of false within the results.
-     * @throws moodle_exception
+     * @throws coding_exception
      */
     public function get_many(array $keys, $strictness = IGNORE_MISSING) {
         if ($this->requirelockingread) {
@@ -1458,6 +1466,7 @@ class cache_application extends cache implements cache_loader_with_locking {
  * @todo we should support locking in the session as well. Should be pretty simple to set up.
  *
  * @internal don't use me directly.
+ * @method cache_store|cache_is_searchable get_store() Returns the cache store which must implement both cache_is_searchable.
  *
  * @package    core
  * @category   cache
@@ -1494,6 +1503,11 @@ class cache_session extends cache {
      */
     const KEY_PREFIX = 'sess_';
 
+    /**
+     * This is the key used to track last access.
+     */
+    const LASTACCESS = '__lastaccess__';
+
     /**
      * Override the cache::construct method.
      *
@@ -1507,12 +1521,15 @@ class cache_session extends cache {
      * @param cache_definition $definition
      * @param cache_store $store
      * @param cache_loader|cache_data_source $loader
-     * @return void
      */
     public function __construct(cache_definition $definition, cache_store $store, $loader = null) {
         // First up copy the loadeduserid to the current user id.
         $this->currentuserid = self::$loadeduserid;
         parent::__construct($definition, $store, $loader);
+
+        // This will trigger check tracked user. If this gets removed a call to that will need to be added here in its place.
+        $this->set(self::LASTACCESS, cache::now());
+
         if ($definition->has_invalidation_events()) {
             $lastinvalidation = $this->get('lastsessioninvalidation');
             if ($lastinvalidation === false) {
@@ -1561,6 +1578,21 @@ class cache_session extends cache {
         }
     }
 
+    /**
+     * Sets the session id for the loader.
+     */
+    protected function set_session_id() {
+        $this->sessionid = preg_replace('#[^a-zA-Z0-9_]#', '_', session_id());
+    }
+
+    /**
+     * Returns the prefix used for all keys.
+     * @return string
+     */
+    protected function get_key_prefix() {
+        return 'u'.$this->currentuserid.'_'.$this->sessionid;
+    }
+
     /**
      * Parses the key turning it into a string (or array is required) suitable to be passed to the cache store.
      *
@@ -1573,17 +1605,18 @@ class cache_session extends cache {
      * @return string|array String unless the store supports multi-identifiers in which case an array if returned.
      */
     protected function parse_key($key) {
-        if ($key === 'lastaccess') {
-            $key = '__lastaccess__';
+        $prefix = $this->get_key_prefix();
+        if ($key === self::LASTACCESS) {
+            return $key.$prefix;
         }
-        return 'sess_'.parent::parse_key($key);
+        return $prefix.'_'.parent::parse_key($key);
     }
 
     /**
      * Check that this cache instance is tracking the current user.
      */
     protected function check_tracked_user() {
-        if (isset($_SESSION['USER']->id)) {
+        if (isset($_SESSION['USER']->id) && $_SESSION['USER']->id !== null) {
             // Get the id of the current user.
             $new = $_SESSION['USER']->id;
         } else {
@@ -1597,55 +1630,25 @@ class cache_session extends cache {
                 // This way we don't bloat the session.
                 $this->purge();
                 // Update the session id just in case!
-                $this->sessionid = session_id();
+                $this->set_session_id();
             }
             self::$loadeduserid = $new;
             $this->currentuserid = $new;
         } else if ($new !== $this->currentuserid) {
             // The current user matches the loaded user but not the user last used by this cache.
-            $this->purge();
+            $this->purge_current_user();
             $this->currentuserid = $new;
             // Update the session id just in case!
-            $this->sessionid = session_id();
+            $this->set_session_id();
         }
     }
 
     /**
-     * Gets the session data.
-     *
-     * @param bool $force If true the session data will be loaded from the store again.
-     * @return array An array of session data.
-     */
-    protected function get_session_data($force = false) {
-        if ($this->sessionid === null) {
-            $this->sessionid = session_id();
-        }
-        if (is_array($this->session) && !$force) {
-            return $this->session;
-        }
-        $session = parent::get($this->sessionid);
-        if ($session === false) {
-            $session = array();
-        }
-        // We have to write here to ensure that the lastaccess time is recorded.
-        // And also in order to ensure the session entry exists as when we save it on __destruct
-        // $CFG is likely to have already been destroyed.
-        $this->save_session($session);
-        return $this->session;
-    }
-
-    /**
-     * Saves the session data.
-     *
-     * This function also updates the last access time.
-     *
-     * @param array $session
-     * @return bool
+     * Purges the session cache of all data belonging to the current user.
      */
-    protected function save_session(array $session) {
-        $session['lastaccess'] = time();
-        $this->session = $session;
-        return parent::set($this->sessionid, $this->session);
+    public function purge_current_user() {
+        $keys = $this->get_store()->find_all($this->get_key_prefix());
+        $this->get_store()->delete_many($keys);
     }
 
     /**
@@ -1656,7 +1659,7 @@ class cache_session extends cache {
      *      In advanced cases an array may be useful such as in situations requiring the multi-key functionality.
      * @param int $strictness One of IGNORE_MISSING | MUST_EXIST
      * @return mixed|false The data from the cache or false if the key did not exist within the cache.
-     * @throws moodle_exception
+     * @throws coding_exception
      */
     public function get($key, $strictness = IGNORE_MISSING) {
         // Check the tracked user.
@@ -1664,10 +1667,8 @@ class cache_session extends cache {
         // 2. Parse the key.
         $parsedkey = $this->parse_key($key);
         // 3. Get it from the store.
-        $result = false;
-        $session = $this->get_session_data();
-        if (array_key_exists($parsedkey, $session)) {
-            $result = $session[$parsedkey];
+        $result = $this->get_store()->get($parsedkey);
+        if ($result !== false) {
             if ($result instanceof cache_ttl_wrapper) {
                 if ($result->has_expired()) {
                     $this->get_store()->delete($parsedkey);
@@ -1681,10 +1682,9 @@ class cache_session extends cache {
             }
         }
         // 4. Load if from the loader/datasource if we don't already have it.
-        $setaftervalidation = false;
         if ($result === false) {
             if ($this->perfdebug) {
-                cache_helper::record_cache_miss('**static session**', $this->get_definition()->get_id());
+                cache_helper::record_cache_miss($this->storetype, $this->get_definition()->get_id());
             }
             if ($this->get_loader() !== false) {
                 // We must pass the original (unparsed) key to the next loader in the chain.
@@ -1694,19 +1694,18 @@ class cache_session extends cache {
             } else if ($this->get_datasource() !== false) {
                 $result = $this->get_datasource()->load_for_cache($key);
             }
-            $setaftervalidation = ($result !== false);
+            // 5. Set it to the store if we got it from the loader/datasource.
+            if ($result !== false) {
+                $this->set($key, $result);
+            }
         } else if ($this->perfdebug) {
-            cache_helper::record_cache_hit('**static session**', $this->get_definition()->get_id());
+            cache_helper::record_cache_hit($this->storetype, $this->get_definition()->get_id());
         }
         // 5. Validate strictness.
         if ($strictness === MUST_EXIST && $result === false) {
-            throw new moodle_exception('Requested key did not exist in any cache stores and could not be loaded.');
-        }
-        // 6. Set it to the store if we got it from the loader/datasource.
-        if ($setaftervalidation) {
-            $this->set($key, $result);
+            throw new coding_exception('Requested key did not exist in any cache stores and could not be loaded.');
         }
-        // 7. Make sure we don't pass back anything that could be a reference.
+        // 6. Make sure we don't pass back anything that could be a reference.
         //    We don't want people modifying the data in the cache.
         if (!is_scalar($result)) {
             // If data is an object it will be a reference.
@@ -1737,8 +1736,14 @@ class cache_session extends cache {
      */
     public function set($key, $data) {
         $this->check_tracked_user();
+        $loader = $this->get_loader();
+        if ($loader !== false) {
+            // We have a loader available set it there as well.
+            // We have to let the loader do its own parsing of data as it may be unique.
+            $loader->set($key, $data);
+        }
         if ($this->perfdebug) {
-            cache_helper::record_cache_set('**static session**', $this->get_definition()->get_id());
+            cache_helper::record_cache_set($this->storetype, $this->get_definition()->get_id());
         }
         if (is_object($data) && $data instanceof cacheable_object) {
             $data = new cache_cached_object($data);
@@ -1750,12 +1755,10 @@ class cache_session extends cache {
             $data = $this->unref($data);
         }
         // We dont' support native TTL here as we consolidate data for sessions.
-        if ($this->has_a_ttl()) {
+        if ($this->has_a_ttl() && !$this->store_supports_native_ttl()) {
             $data = new cache_ttl_wrapper($data, $this->get_definition()->get_ttl());
         }
-        $session = $this->get_session_data();
-        $session[$this->parse_key($key)] = $data;
-        return $this->save_session($session);
+        return $this->get_store()->set($this->parse_key($key), $data);
     }
 
     /**
@@ -1767,15 +1770,12 @@ class cache_session extends cache {
      * @return bool True of success, false otherwise.
      */
     public function delete($key, $recurse = true) {
-        $this->check_tracked_user();
         $parsedkey = $this->parse_key($key);
         if ($recurse && $this->get_loader() !== false) {
             // Delete from the bottom of the stack first.
             $this->get_loader()->delete($key, $recurse);
         }
-        $session = $this->get_session_data();
-        unset($session[$parsedkey]);
-        return $this->save_session($session);
+        return $this->get_store()->delete($parsedkey);
     }
 
     /**
@@ -1794,15 +1794,72 @@ class cache_session extends cache {
      * @return array An array of key value pairs for the items that could be retrieved from the cache.
      *      If MUST_EXIST was used and not all keys existed within the cache then an exception will be thrown.
      *      Otherwise any key that did not exist will have a data value of false within the results.
-     * @throws moodle_exception
+     * @throws coding_exception
      */
     public function get_many(array $keys, $strictness = IGNORE_MISSING) {
         $this->check_tracked_user();
-        $return = array();
+        $parsedkeys = array();
+        $keymap = array();
         foreach ($keys as $key) {
-            $return[$key] = $this->get($key, $strictness);
+            $parsedkey = $this->parse_key($key);
+            $parsedkeys[$key] = $parsedkey;
+            $keymap[$parsedkey] = $key;
+        }
+        $result = $this->get_store()->get_many($parsedkeys);
+        $return = array();
+        $missingkeys = array();
+        $hasmissingkeys = false;
+        foreach ($result as $parsedkey => $value) {
+            $key = $keymap[$parsedkey];
+            if ($value instanceof cache_ttl_wrapper) {
+                /* @var cache_ttl_wrapper $value */
+                if ($value->has_expired()) {
+                    $this->delete($keymap[$parsedkey]);
+                    $value = false;
+                } else {
+                    $value = $value->data;
+                }
+            }
+            if ($value instanceof cache_cached_object) {
+                /* @var cache_cached_object $value */
+                $value = $value->restore_object();
+            }
+            $return[$key] = $value;
+            if ($value === false) {
+                $hasmissingkeys = true;
+                $missingkeys[$parsedkey] = $key;
+            }
+        }
+        if ($hasmissingkeys) {
+            // We've got missing keys - we've got to check any loaders or data sources.
+            $loader = $this->get_loader();
+            $datasource = $this->get_datasource();
+            if ($loader !== false) {
+                foreach ($loader->get_many($missingkeys) as $key => $value) {
+                    if ($value !== false) {
+                        $return[$key] = $value;
+                        unset($missingkeys[$parsedkeys[$key]]);
+                    }
+                }
+            }
+            $hasmissingkeys = count($missingkeys) > 0;
+            if ($datasource !== false && $hasmissingkeys) {
+                // We're still missing keys but we've got a datasource.
+                foreach ($datasource->load_many_for_cache($missingkeys) as $key => $value) {
+                    if ($value !== false) {
+                        $return[$key] = $value;
+                        unset($missingkeys[$parsedkeys[$key]]);
+                    }
+                }
+                $hasmissingkeys = count($missingkeys) > 0;
+            }
         }
+        if ($hasmissingkeys && $strictness === MUST_EXIST) {
+            throw new coding_exception('Requested key did not exist in any cache stores and could not be loaded.');
+        }
+
         return $return;
+
     }
 
     /**
@@ -1814,18 +1871,12 @@ class cache_session extends cache {
      * @return int The number of items successfully deleted.
      */
     public function delete_many(array $keys, $recurse = true) {
-        $this->check_tracked_user();
         $parsedkeys = array_map(array($this, 'parse_key'), $keys);
         if ($recurse && $this->get_loader() !== false) {
             // Delete from the bottom of the stack first.
             $this->get_loader()->delete_many($keys, $recurse);
         }
-        $session = $this->get_session_data();
-        foreach ($parsedkeys as $parsedkey) {
-            unset($session[$parsedkey]);
-        }
-        $this->save_session($session);
-        return count($keys);
+        return $this->get_store()->delete_many($parsedkeys);
     }
 
     /**
@@ -1853,8 +1904,15 @@ class cache_session extends cache {
      */
     public function set_many(array $keyvaluearray) {
         $this->check_tracked_user();
-        $session = $this->get_session_data();
-        $simulatettl = $this->has_a_ttl();
+        $loader = $this->get_loader();
+        if ($loader !== false) {
+            // We have a loader available set it there as well.
+            // We have to let the loader do its own parsing of data as it may be unique.
+            $loader->set_many($keyvaluearray);
+        }
+        $data = array();
+        $definitionid = $this->get_definition()->get_ttl();
+        $simulatettl = $this->has_a_ttl() && !$this->store_supports_native_ttl();
         foreach ($keyvaluearray as $key => $value) {
             if (is_object($value) && $value instanceof cacheable_object) {
                 $value = new cache_cached_object($value);
@@ -1866,16 +1924,17 @@ class cache_session extends cache {
                 $value = $this->unref($value);
             }
             if ($simulatettl) {
-                $value = new cache_ttl_wrapper($value, $this->get_definition()->get_ttl());
+                $value = new cache_ttl_wrapper($value, $definitionid);
             }
-            $parsedkey = $this->parse_key($key);
-            $session[$parsedkey] = $value;
+            $data[$key] = array(
+                'key' => $this->parse_key($key),
+                'value' => $value
+            );
         }
         if ($this->perfdebug) {
-            cache_helper::record_cache_set($this->storetype, $this->get_definition()->get_id());
+            cache_helper::record_cache_set($this->storetype, $definitionid);
         }
-        $this->save_session($session);
-        return count($keyvaluearray);
+        return $this->get_store()->set_many($data);
     }
 
     /**
@@ -1884,13 +1943,9 @@ class cache_session extends cache {
      * @return bool True on success, false otherwise
      */
     public function purge() {
-        // 1. Purge the session object.
-        $this->session = array();
-        // 2. Delete the record for this users session from the store.
-        $this->get_store()->delete($this->sessionid);
-        // 3. Optionally purge any stacked loaders in the same way.
+        $this->get_store()->purge();
         if ($this->get_loader()) {
-            $this->get_loader()->delete($this->sessionid);
+            $this->get_loader()->purge();
         }
         return true;
     }
@@ -1919,21 +1974,27 @@ class cache_session extends cache {
     public function has($key, $tryloadifpossible = false) {
         $this->check_tracked_user();
         $parsedkey = $this->parse_key($key);
-        $session = $this->get_session_data();
-        $has = false;
-        if ($this->has_a_ttl()) {
+        $store = $this->get_store();
+        if ($this->has_a_ttl() && !$this->store_supports_native_ttl()) {
             // The data has a TTL and the store doesn't support it natively.
             // We must fetch the data and expect a ttl wrapper.
-            if (array_key_exists($parsedkey, $session)) {
-                $data = $session[$parsedkey];
-                $has = ($data instanceof cache_ttl_wrapper && !$data->has_expired());
-            }
+            $data = $store->get($parsedkey);
+            $has = ($data instanceof cache_ttl_wrapper && !$data->has_expired());
+        } else if (!$this->store_supports_key_awareness()) {
+            // The store doesn't support key awareness, get the data and check it manually... puke.
+            // Either no TTL is set of the store supports its handling natively.
+            $data = $store->get($parsedkey);
+            $has = ($data !== false);
         } else {
-            $has = array_key_exists($parsedkey, $session);
+            // The store supports key awareness, this is easy!
+            // Either no TTL is set of the store supports its handling natively.
+            /* @var cache_store|cache_is_key_aware $store */
+            $has = $store->has($parsedkey);
         }
         if (!$has && $tryloadifpossible) {
+            $result = null;
             if ($this->get_loader() !== false) {
-                $result = $this->get_loader()->get($key);
+                $result = $this->get_loader()->get($parsedkey);
             } else if ($this->get_datasource() !== null) {
                 $result = $this->get_datasource()->load_for_cache($key);
             }
@@ -1960,25 +2021,18 @@ class cache_session extends cache {
      */
     public function has_all(array $keys) {
         $this->check_tracked_user();
-        $session = $this->get_session_data();
-        foreach ($keys as $key) {
-            $has = false;
-            $parsedkey = $this->parse_key($key);
-            if ($this->has_a_ttl()) {
-                // The data has a TTL and the store doesn't support it natively.
-                // We must fetch the data and expect a ttl wrapper.
-                if (array_key_exists($parsedkey, $session)) {
-                    $data = $session[$parsedkey];
-                    $has = ($data instanceof cache_ttl_wrapper && !$data->has_expired());
+        if (($this->has_a_ttl() && !$this->store_supports_native_ttl()) || !$this->store_supports_key_awareness()) {
+            foreach ($keys as $key) {
+                if (!$this->has($key)) {
+                    return false;
                 }
-            } else {
-                $has = array_key_exists($parsedkey, $session);
-            }
-            if (!$has) {
-                return false;
             }
+            return true;
         }
-        return true;
+        // The cache must be key aware and if support native ttl if it a ttl is set.
+        /* @var cache_store|cache_is_key_aware $store */
+        $store = $this->get_store();
+        return $store->has_all(array_map(array($this, 'parse_key'), $keys));
     }
 
     /**
@@ -1995,26 +2049,17 @@ class cache_session extends cache {
      * @return bool True if the cache has at least one of the given keys
      */
     public function has_any(array $keys) {
-        $this->check_tracked_user();
-        $session = $this->get_session_data();
-        foreach ($keys as $key) {
-            $has = false;
-            $parsedkey = $this->parse_key($key);
-            if ($this->has_a_ttl()) {
-                // The data has a TTL and the store doesn't support it natively.
-                // We must fetch the data and expect a ttl wrapper.
-                if (array_key_exists($parsedkey, $session)) {
-                    $data = $session[$parsedkey];
-                    $has = ($data instanceof cache_ttl_wrapper && !$data->has_expired());
+        if (($this->has_a_ttl() && !$this->store_supports_native_ttl()) || !$this->store_supports_key_awareness()) {
+            foreach ($keys as $key) {
+                if ($this->has($key)) {
+                    return true;
                 }
-            } else {
-                $has = array_key_exists($parsedkey, $session);
-            }
-            if ($has) {
-                return true;
             }
+            return false;
         }
-        return false;
+        /* @var cache_store|cache_is_key_aware $store */
+        $store = $this->get_store();
+        return $store->has_any(array_map(array($this, 'parse_key'), $keys));
     }
 
     /**
index 4830d71..514a4bd 100644 (file)
@@ -353,23 +353,19 @@ class cachestore_file extends cache_store implements cache_is_key_aware, cache_i
         if (!$readfile) {
             return false;
         }
-        // Check the filesize first, likely not needed but important none the less.
-        $filesize = filesize($file);
-        if (!$filesize) {
-            return false;
-        }
-        // Open ensuring the file for writing, truncating it and setting the pointer to the start.
+        // Open ensuring the file for reading in binary format.
         if (!$handle = fopen($file, 'rb')) {
             return false;
         }
         // Lock it up!
         // We don't care if this succeeds or not, on some systems it will, on some it won't, meah either way.
         flock($handle, LOCK_SH);
-        // HACK ALERT
-        // There is a problem when reading from the file during PHPUNIT tests. For one reason or another the filesize is not correct
-        // Doesn't happen during normal operation, just during unit tests.
-        // Read it.
-        $data = fread($handle, $filesize+128);
+        $data = '';
+        // Read the data in 1Mb chunks. Small caches will not loop more than once.  We don't use filesize as it may
+        // be cached with a different value than what we need to read from the file.
+        do {
+            $data .= fread($handle, 1048576);
+        } while (!feof($handle));
         // Unlock it.
         flock($handle, LOCK_UN);
         // Return it unserialised.
index 2e104bd..1569da6 100644 (file)
@@ -132,8 +132,8 @@ class cachestore_memcache extends cache_store implements cache_is_configurable {
         $this->connection = new Memcache;
         foreach ($this->servers as $server) {
             $this->connection->addServer($server[0], $server[1], true, $server[2]);
-            // Test the connection to this server.
         }
+        // Test the connection to the pool of servers.
         $this->isready = @$this->connection->set($this->parse_key('ping'), 'ping', MEMCACHE_COMPRESSED, 1);
     }
 
index a74dc83..508628e 100644 (file)
@@ -141,6 +141,7 @@ class cachestore_memcached extends cache_store implements cache_is_configurable
             }
             $this->connection->addServers($this->servers);
         }
+        // Test the connection to the pool of servers.
         $this->isready = @$this->connection->set("ping", 'ping', 1);
     }
 
index 8221502..d5e245e 100644 (file)
@@ -118,7 +118,7 @@ class cachestore_session extends session_data_store implements cache_is_key_awar
 
     /**
      * The maximum size for the store, or false if there isn't one.
-     * @var bool
+     * @var bool|int
      */
     protected $maxsize = false;
 
@@ -202,7 +202,7 @@ class cachestore_session extends session_data_store implements cache_is_key_awar
      */
     public function initialise(cache_definition $definition) {
         $this->storeid = $definition->generate_definition_hash();
-        $this->store = &self::register_store_id($definition->get_id());
+        $this->store = &self::register_store_id($this->name.'-'.$definition->get_id());
         $this->ttl = $definition->get_ttl();
         $maxsize = $definition->get_maxsize();
         if ($maxsize !== null) {
@@ -210,6 +210,7 @@ class cachestore_session extends session_data_store implements cache_is_key_awar
             $this->maxsize = abs((int)$maxsize);
             $this->storecount = count($this->store);
         }
+        $this->check_ttl();
     }
 
     /**
@@ -238,9 +239,17 @@ class cachestore_session extends session_data_store implements cache_is_key_awar
     public function get($key) {
         if (isset($this->store[$key])) {
             if ($this->ttl == 0) {
-                return $this->store[$key][0];
+                $value = $this->store[$key][0];
+                if ($this->maxsize !== false) {
+                    // Make sure the element is now in the end of array.
+                    $this->set($key, $value);
+                }
+                return $value;
             } else if ($this->store[$key][1] >= (cache::now() - $this->ttl)) {
                 return $this->store[$key][0];
+            } else {
+                // Element is present but has expired.
+                $this->check_ttl();
             }
         }
         return false;
@@ -257,20 +266,32 @@ class cachestore_session extends session_data_store implements cache_is_key_awar
      */
     public function get_many($keys) {
         $return = array();
+        $maxtime = 0;
         if ($this->ttl != 0) {
             $maxtime = cache::now() - $this->ttl;
         }
 
+        $hasexpiredelements = false;
         foreach ($keys as $key) {
             $return[$key] = false;
             if (isset($this->store[$key])) {
                 if ($this->ttl == 0) {
                     $return[$key] = $this->store[$key][0];
+                    if ($this->maxsize !== false) {
+                        // Make sure the element is now in the end of array.
+                        $this->set($key, $return[$key], false);
+                    }
                 } else if ($this->store[$key][1] >= $maxtime) {
                     $return[$key] = $this->store[$key][0];
+                } else {
+                    $hasexpiredelements = true;
                 }
             }
         }
+        if ($hasexpiredelements) {
+            // There are some elements that are present but have expired.
+            $this->check_ttl();
+        }
         return $return;
     }
 
@@ -279,24 +300,27 @@ class cachestore_session extends session_data_store implements cache_is_key_awar
      *
      * @param string $key The key to use.
      * @param mixed $data The data to set.
-     * @param bool $testmaxsize If set to true then we test the maxsize arg and reduce if required.
+     * @param bool $testmaxsize If set to true then we test the maxsize arg and reduce if required. If this is set to false you will
+     *      need to perform these checks yourself. This allows for bulk set's to be performed and maxsize tests performed once.
      * @return bool True if the operation was a success false otherwise.
      */
     public function set($key, $data, $testmaxsize = true) {
         $testmaxsize = ($testmaxsize && $this->maxsize !== false);
-        if ($testmaxsize) {
-            $increment = (!isset($this->store[$key]));
+        $increment = $this->maxsize !== false && !isset($this->store[$key]);
+        if (($this->maxsize !== false && !$increment) || $this->ttl != 0) {
+            // Make sure the element is added to the end of $this->store array.
+            unset($this->store[$key]);
         }
-        if ($this->ttl == 0) {
-            $this->store[$key][0] = $data;
+        if ($this->ttl === 0) {
+            $this->store[$key] = array($data, 0);
         } else {
             $this->store[$key] = array($data, cache::now());
         }
-        if ($testmaxsize && $increment) {
+        if ($increment) {
             $this->storecount++;
-            if ($this->storecount > $this->maxsize) {
-                $this->reduce_for_maxsize();
-            }
+        }
+        if ($testmaxsize && $this->storecount > $this->maxsize) {
+            $this->reduce_for_maxsize();
         }
         return true;
     }
@@ -311,12 +335,26 @@ class cachestore_session extends session_data_store implements cache_is_key_awar
      */
     public function set_many(array $keyvaluearray) {
         $count = 0;
+        $increment = 0;
         foreach ($keyvaluearray as $pair) {
-            $this->set($pair['key'], $pair['value'], false);
+            $key = $pair['key'];
+            $data = $pair['value'];
             $count++;
+            if ($this->maxsize !== false || $this->ttl !== 0) {
+                // Make sure the element is added to the end of $this->store array.
+                $this->delete($key);
+                $increment++;
+            } else if (!isset($this->store[$key])) {
+                $increment++;
+            }
+            if ($this->ttl === 0) {
+                $this->store[$key] = array($data, 0);
+            } else {
+                $this->store[$key] = array($data, cache::now());
+            }
         }
         if ($this->maxsize !== false) {
-            $this->storecount += $count;
+            $this->storecount += $increment;
             if ($this->storecount > $this->maxsize) {
                 $this->reduce_for_maxsize();
             }
@@ -348,6 +386,7 @@ class cachestore_session extends session_data_store implements cache_is_key_awar
      * @return bool
      */
     public function has_all(array $keys) {
+        $maxtime = 0;
         if ($this->ttl != 0) {
             $maxtime = cache::now() - $this->ttl;
         }
@@ -370,6 +409,7 @@ class cachestore_session extends session_data_store implements cache_is_key_awar
      * @return bool
      */
     public function has_any(array $keys) {
+        $maxtime = 0;
         if ($this->ttl != 0) {
             $maxtime = cache::now() - $this->ttl;
         }
@@ -389,12 +429,14 @@ class cachestore_session extends session_data_store implements cache_is_key_awar
      * @return bool Returns true if the operation was a success, false otherwise.
      */
     public function delete($key) {
-        $result = isset($this->store[$key]);
+        if (!isset($this->store[$key])) {
+            return false;
+        }
         unset($this->store[$key]);
         if ($this->maxsize !== false) {
             $this->storecount--;
         }
-        return $result;
+        return true;
     }
 
     /**
@@ -404,17 +446,18 @@ class cachestore_session extends session_data_store implements cache_is_key_awar
      * @return int The number of items successfully deleted.
      */
     public function delete_many(array $keys) {
-        $count = 0;
+        // The number of items that have actually being removed.
+        $reduction = 0;
         foreach ($keys as $key) {
             if (isset($this->store[$key])) {
-                $count++;
+                $reduction++;
             }
             unset($this->store[$key]);
         }
         if ($this->maxsize !== false) {
-            $this->storecount -= $count;
+            $this->storecount -= $reduction;
         }
-        return $count;
+        return $reduction;
     }
 
     /**
@@ -489,12 +532,40 @@ class cachestore_session extends session_data_store implements cache_is_key_awar
         return $this->name;
     }
 
+    /**
+     * Removes expired elements.
+     * @return int number of removed elements
+     */
+    protected function check_ttl() {
+        if ($this->ttl === 0) {
+            return 0;
+        }
+        $maxtime = cache::now() - $this->ttl;
+        $count = 0;
+        for ($value = reset($this->store); $value !== false; $value = next($this->store)) {
+            if ($value[1] >= $maxtime) {
+                // We know that elements are sorted by ttl so no need to continue.
+                break;
+            }
+            $count++;
+        }
+        if ($count) {
+            // Remove first $count elements as they are expired.
+            $this->store = array_slice($this->store, $count, null, true);
+            if ($this->maxsize !== false) {
+                $this->storecount -= $count;
+            }
+        }
+        return $count;
+    }
+
     /**
      * Finds all of the keys being stored in the cache store instance.
      *
      * @return array
      */
     public function find_all() {
+        $this->check_ttl();
         return array_keys($this->store);
     }
 
@@ -502,6 +573,7 @@ class cachestore_session extends session_data_store implements cache_is_key_awar
      * Finds all of the keys whose keys start with the given prefix.
      *
      * @param string $prefix
+     * @return array An array of keys.
      */
     public function find_by_prefix($prefix) {
         $return = array();
@@ -512,4 +584,12 @@ class cachestore_session extends session_data_store implements cache_is_key_awar
         }
         return $return;
     }
+
+    /**
+     * This store supports native TTL handling.
+     * @return bool
+     */
+    public function store_supports_native_ttl() {
+        return true;
+    }
 }
index 3ee20f2..ebf7059 100644 (file)
@@ -49,60 +49,66 @@ class cachestore_session_test extends cachestore_tests {
      * Test the maxsize option.
      */
     public function test_maxsize() {
-        $defid = 'phpunit/testmaxsize';
         $config = cache_config_phpunittest::instance();
-        $config->phpunit_add_definition($defid, array(
+        $config->phpunit_add_definition('phpunit/one', array(
             'mode' => cache_store::MODE_SESSION,
             'component' => 'phpunit',
-            'area' => 'testmaxsize',
+            'area' => 'one',
             'maxsize' => 3
         ));
-        $definition = cache_definition::load($defid, $config->get_definition_by_id($defid));
-        $instance = cachestore_session::initialise_test_instance($definition);
 
-        $this->assertTrue($instance->set('key1', 'value1'));
-        $this->assertTrue($instance->set('key2', 'value2'));
-        $this->assertTrue($instance->set('key3', 'value3'));
+        $config->phpunit_add_definition('phpunit/two', array(
+            'mode' => cache_store::MODE_SESSION,
+            'component' => 'phpunit',
+            'area' => 'two',
+            'maxsize' => 3
+        ));
+
+        $cacheone = cache::make('phpunit', 'one');
 
-        $this->assertTrue($instance->has('key1'));
-        $this->assertTrue($instance->has('key2'));
-        $this->assertTrue($instance->has('key3'));
+        $this->assertTrue($cacheone->set('key1', 'value1'));
+        $this->assertTrue($cacheone->set('key2', 'value2'));
+        $this->assertTrue($cacheone->set('key3', 'value3'));
 
-        $this->assertTrue($instance->set('key4', 'value4'));
-        $this->assertTrue($instance->set('key5', 'value5'));
+        $this->assertTrue($cacheone->has('key1'));
+        $this->assertTrue($cacheone->has('key2'));
+        $this->assertTrue($cacheone->has('key3'));
 
-        $this->assertFalse($instance->has('key1'));
-        $this->assertFalse($instance->has('key2'));
-        $this->assertTrue($instance->has('key3'));
-        $this->assertTrue($instance->has('key4'));
-        $this->assertTrue($instance->has('key5'));
+        $this->assertTrue($cacheone->set('key4', 'value4'));
+        $this->assertTrue($cacheone->set('key5', 'value5'));
 
-        $this->assertFalse($instance->get('key1'));
-        $this->assertFalse($instance->get('key2'));
-        $this->assertEquals('value3', $instance->get('key3'));
-        $this->assertEquals('value4', $instance->get('key4'));
-        $this->assertEquals('value5', $instance->get('key5'));
+        $this->assertFalse($cacheone->has('key1'));
+        $this->assertFalse($cacheone->has('key2'));
+        $this->assertTrue($cacheone->has('key3'));
+        $this->assertTrue($cacheone->has('key4'));
+        $this->assertTrue($cacheone->has('key5'));
+
+        $this->assertFalse($cacheone->get('key1'));
+        $this->assertFalse($cacheone->get('key2'));
+        $this->assertEquals('value3', $cacheone->get('key3'));
+        $this->assertEquals('value4', $cacheone->get('key4'));
+        $this->assertEquals('value5', $cacheone->get('key5'));
 
         // Test adding one more.
-        $this->assertTrue($instance->set('key6', 'value6'));
-        $this->assertFalse($instance->get('key3'));
+        $this->assertTrue($cacheone->set('key6', 'value6'));
+        $this->assertFalse($cacheone->get('key3'));
 
         // Test reducing and then adding to make sure we don't lost one.
-        $this->assertTrue($instance->delete('key6'));
-        $this->assertTrue($instance->set('key7', 'value7'));
-        $this->assertEquals('value4', $instance->get('key4'));
+        $this->assertTrue($cacheone->delete('key6'));
+        $this->assertTrue($cacheone->set('key7', 'value7'));
+        $this->assertEquals('value4', $cacheone->get('key4'));
 
         // Set the same key three times to make sure it doesn't count overrides.
         for ($i = 0; $i < 3; $i++) {
-            $this->assertTrue($instance->set('key8', 'value8'));
+            $this->assertTrue($cacheone->set('key8', 'value8'));
         }
-        $this->assertEquals('value7', $instance->get('key7'), 'Overrides are incorrectly incrementing size');
+        $this->assertEquals('value7', $cacheone->get('key7'), 'Overrides are incorrectly incrementing size');
 
         // Test adding many.
-        $this->assertEquals(3, $instance->set_many(array(
-            array('key' => 'keyA', 'value' => 'valueA'),
-            array('key' => 'keyB', 'value' => 'valueB'),
-            array('key' => 'keyC', 'value' => 'valueC')
+        $this->assertEquals(3, $cacheone->set_many(array(
+            'keyA' => 'valueA',
+            'keyB' => 'valueB',
+            'keyC' => 'valueC'
         )));
         $this->assertEquals(array(
             'key4' => false,
@@ -112,8 +118,82 @@ class cachestore_session_test extends cachestore_tests {
             'keyA' => 'valueA',
             'keyB' => 'valueB',
             'keyC' => 'valueC'
-        ), $instance->get_many(array(
+        ), $cacheone->get_many(array(
             'key4', 'key5', 'key6', 'key7', 'keyA', 'keyB', 'keyC'
         )));
+
+        $cachetwo = cache::make('phpunit', 'two');
+
+        // Test adding many.
+        $this->assertEquals(3, $cacheone->set_many(array(
+            'keyA' => 'valueA',
+            'keyB' => 'valueB',
+            'keyC' => 'valueC'
+        )));
+
+        $this->assertEquals(3, $cachetwo->set_many(array(
+            'key1' => 'value1',
+            'key2' => 'value2',
+            'key3' => 'value3'
+        )));
+
+        $this->assertEquals(array(
+            'keyA' => 'valueA',
+            'keyB' => 'valueB',
+            'keyC' => 'valueC'
+        ), $cacheone->get_many(array(
+            'keyA', 'keyB', 'keyC'
+        )));
+
+        $this->assertEquals(array(
+            'key1' => 'value1',
+            'key2' => 'value2',
+            'key3' => 'value3'
+        ), $cachetwo->get_many(array(
+            'key1', 'key2', 'key3'
+        )));
+
+        // Test that that cache deletes element that was least recently accessed.
+        $this->assertEquals('valueA', $cacheone->get('keyA'));
+        $cacheone->set('keyD', 'valueD');
+        $this->assertEquals('valueA', $cacheone->get('keyA'));
+        $this->assertFalse($cacheone->get('keyB'));
+        $this->assertEquals(array('keyD' => 'valueD', 'keyC' => 'valueC'), $cacheone->get_many(array('keyD', 'keyC')));
+        $cacheone->set('keyE', 'valueE');
+        $this->assertFalse($cacheone->get('keyB'));
+        $this->assertFalse($cacheone->get('keyA'));
+        $this->assertEquals(array('keyA' => false, 'keyE' => 'valueE', 'keyD' => 'valueD', 'keyC' => 'valueC'),
+                $cacheone->get_many(array('keyA', 'keyE', 'keyD', 'keyC')));
+        // Overwrite keyE (moves it to the end of array), and set keyF.
+        $cacheone->set_many(array('keyE' => 'valueE', 'keyF' => 'valueF'));
+        $this->assertEquals(array('keyC' => 'valueC', 'keyE' => 'valueE', 'keyD' => false, 'keyF' => 'valueF'),
+                $cacheone->get_many(array('keyC', 'keyE', 'keyD', 'keyF')));
+    }
+
+    public function test_ttl() {
+        $config = cache_config_phpunittest::instance();
+        $config->phpunit_add_definition('phpunit/three', array(
+            'mode' => cache_store::MODE_SESSION,
+            'component' => 'phpunit',
+            'area' => 'three',
+            'maxsize' => 3,
+            'ttl' => 3
+        ));
+
+        $cachethree = cache::make('phpunit', 'three');
+
+        // Make sure that when cache with ttl is full the elements that were added first are deleted first regardless of access time.
+        $cachethree->set('key1', 'value1');
+        $cachethree->set('key2', 'value2');
+        $cachethree->set('key3', 'value3');
+        $cachethree->set('key4', 'value4');
+        $this->assertFalse($cachethree->get('key1'));
+        $this->assertEquals('value4', $cachethree->get('key4'));
+        $cachethree->set('key5', 'value5');
+        $this->assertFalse($cachethree->get('key2'));
+        $this->assertEquals('value4', $cachethree->get('key4'));
+        $cachethree->set_many(array('key6' => 'value6', 'key7' => 'value7'));
+        $this->assertEquals(array('key3' => false, 'key4' => false, 'key5' => 'value5', 'key6' => 'value6', 'key7' => 'value7'),
+                $cachethree->get_many(array('key3', 'key4', 'key5', 'key6', 'key7')));
     }
 }
\ No newline at end of file
index e7f4cdc..23df737 100644 (file)
@@ -1126,9 +1126,9 @@ class core_cache_testcase extends advanced_testcase {
     }
 
     /**
-     * Test that multiple loaders work ok.
+     * Test that multiple application loaders work ok.
      */
-    public function test_multiple_loaders() {
+    public function test_multiple_application_loaders() {
         $instance = cache_config_phpunittest::instance(true);
         $instance->phpunit_add_file_store('phpunittest1');
         $instance->phpunit_add_file_store('phpunittest2');
@@ -1173,6 +1173,93 @@ class core_cache_testcase extends advanced_testcase {
         $this->assertFalse($result['a']);
         $this->assertEquals('B', $result['b']);
         $this->assertFalse($result['c']);
+
+        // Test non-recursive deletes.
+        $this->assertTrue($cache->set('test', 'test'));
+        $this->assertSame('test', $cache->get('test'));
+        $this->assertTrue($cache->delete('test', false));
+        // We should still have it on a deeper loader.
+        $this->assertSame('test', $cache->get('test'));
+        // Test non-recusive with many functions.
+        $this->assertSame(3, $cache->set_many(array(
+            'one' => 'one',
+            'two' => 'two',
+            'three' => 'three'
+        )));
+        $this->assertSame('one', $cache->get('one'));
+        $this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
+        $this->assertSame(3, $cache->delete_many(array('one', 'two', 'three'), false));
+        $this->assertSame('one', $cache->get('one'));
+        $this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
+    }
+
+    /**
+     * Test that multiple application loaders work ok.
+     */
+    public function test_multiple_session_loaders() {
+        /* @var cache_config_phpunittest $instance */
+        $instance = cache_config_phpunittest::instance(true);
+        $instance->phpunit_add_session_store('phpunittest1');
+        $instance->phpunit_add_session_store('phpunittest2');
+        $instance->phpunit_add_definition('phpunit/multi_loader', array(
+            'mode' => cache_store::MODE_SESSION,
+            'component' => 'phpunit',
+            'area' => 'multi_loader'
+        ));
+        $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest1', 3);
+        $instance->phpunit_add_definition_mapping('phpunit/multi_loader', 'phpunittest2', 2);
+
+        $cache = cache::make('phpunit', 'multi_loader');
+        $this->assertInstanceOf('cache_session', $cache);
+        $this->assertFalse($cache->get('test'));
+        $this->assertTrue($cache->set('test', 'test'));
+        $this->assertEquals('test', $cache->get('test'));
+        $this->assertTrue($cache->delete('test'));
+        $this->assertFalse($cache->get('test'));
+        $this->assertTrue($cache->set('test', 'test'));
+        $this->assertTrue($cache->purge());
+        $this->assertFalse($cache->get('test'));
+
+        // Test the many commands.
+        $this->assertEquals(3, $cache->set_many(array('a' => 'A', 'b' => 'B', 'c' => 'C')));
+        $result = $cache->get_many(array('a', 'b', 'c'));
+        $this->assertInternalType('array', $result);
+        $this->assertCount(3, $result);
+        $this->assertArrayHasKey('a', $result);
+        $this->assertArrayHasKey('b', $result);
+        $this->assertArrayHasKey('c', $result);
+        $this->assertEquals('A', $result['a']);
+        $this->assertEquals('B', $result['b']);
+        $this->assertEquals('C', $result['c']);
+        $this->assertEquals($result, $cache->get_many(array('a', 'b', 'c')));
+        $this->assertEquals(2, $cache->delete_many(array('a', 'c')));
+        $result = $cache->get_many(array('a', 'b', 'c'));
+        $this->assertInternalType('array', $result);
+        $this->assertCount(3, $result);
+        $this->assertArrayHasKey('a', $result);
+        $this->assertArrayHasKey('b', $result);
+        $this->assertArrayHasKey('c', $result);
+        $this->assertFalse($result['a']);
+        $this->assertEquals('B', $result['b']);
+        $this->assertFalse($result['c']);
+
+        // Test non-recursive deletes.
+        $this->assertTrue($cache->set('test', 'test'));
+        $this->assertSame('test', $cache->get('test'));
+        $this->assertTrue($cache->delete('test', false));
+        // We should still have it on a deeper loader.
+        $this->assertSame('test', $cache->get('test'));
+        // Test non-recusive with many functions.
+        $this->assertSame(3, $cache->set_many(array(
+            'one' => 'one',
+            'two' => 'two',
+            'three' => 'three'
+        )));
+        $this->assertSame('one', $cache->get('one'));
+        $this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
+        $this->assertSame(3, $cache->delete_many(array('one', 'two', 'three'), false));
+        $this->assertSame('one', $cache->get('one'));
+        $this->assertSame(array('two' => 'two', 'three' => 'three'), $cache->get_many(array('two', 'three')));
     }
 
     /**
@@ -1448,4 +1535,4 @@ class core_cache_testcase extends advanced_testcase {
         $this->assertInstanceOf('cache_request', $cache);
         $this->assertArrayHasKey('cache_is_searchable', $cache->phpunit_get_store_implements());
     }
-}
+}
\ No newline at end of file
index f03a66c..b6d7358 100644 (file)
@@ -94,6 +94,24 @@ class cache_config_phpunittest extends cache_config_writer {
         );
     }
 
+    /**
+     * Forcefully adds a session store.
+     *
+     * @param string $name
+     */
+    public function phpunit_add_session_store($name) {
+        $this->configstores[$name] = array(
+            'name' => $name,
+            'plugin' => 'session',
+            'configuration' => array(),
+            'features' => 14,
+            'modes' => 2,
+            'default' => true,
+            'class' => 'cachestore_session',
+            'lock' => 'cachelock_file_default',
+        );
+    }
+
     /**
      * Forcefully injects a definition => store mapping.
      *
diff --git a/course/format/scorm/format.php b/course/format/scorm/format.php
deleted file mode 100644 (file)
index 465f7f7..0000000
+++ /dev/null
@@ -1,17 +0,0 @@
-<?php
-      // format.php - course format featuring single activity
-      //              included from view.php
-
-    $module = $course->format;
-    require_once($CFG->dirroot.'/mod/'.$module.'/locallib.php');
-
-    $strgroups  = get_string('groups');
-    $strgroupmy = get_string('groupmy');
-    $editing    = $PAGE->user_is_editing();
-
-    $moduleformat = $module.'_course_format_display';
-    if (function_exists($moduleformat)) {
-        $moduleformat($USER,$course);
-    } else {
-        echo $OUTPUT->notification('The module '. $module. ' does not support single activity course format');
-    }
diff --git a/course/format/scorm/lib.php b/course/format/scorm/lib.php
deleted file mode 100644 (file)
index 60e9a26..0000000
+++ /dev/null
@@ -1,101 +0,0 @@
-<?php
-// This file is part of Moodle - http://moodle.org/
-//
-// Moodle is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-//
-// Moodle is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
-
-/**
- * This file contains main class for the course format SCORM
- *
- * @since     2.0
- * @package   format_scorm
- * @copyright 2009 Sam Hemelryk
- * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
-
-defined('MOODLE_INTERNAL') || die();
-require_once($CFG->dirroot. '/course/format/lib.php');
-
-/**
- * Main class for the Scorm course format
- *
- * @package    format_scorm
- * @copyright  2012 Marina Glancy
- * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
- */
-class format_scorm extends format_base {
-
-    /**
-     * The URL to use for the specified course
-     *
-     * @param int|stdClass $section Section object from database or just field course_sections.section
-     *     if null the course view page is returned
-     * @param array $options options for view URL. At the moment core uses:
-     *     'navigation' (bool) if true and section has no separate page, the function returns null
-     *     'sr' (int) used by multipage formats to specify to which section to return
-     * @return null|moodle_url
-     */
-    public function get_view_url($section, $options = array()) {
-        if (!empty($options['navigation']) && $section !== null) {
-            return null;
-        }
-        return new moodle_url('/course/view.php', array('id' => $this->courseid));
-    }
-
-    /**
-     * Loads all of the course sections into the navigation
-     *
-     * @param global_navigation $navigation
-     * @param navigation_node $node The course node within the navigation
-     */
-    public function extend_course_navigation($navigation, navigation_node $node) {
-        // Scorm course format does not extend course navigation
-    }
-
-    /**
-     * Returns the list of blocks to be automatically added for the newly created course
-     *
-     * @return array of default blocks, must contain two keys BLOCK_POS_LEFT and BLOCK_POS_RIGHT
-     *     each of values is an array of block names (for left and right side columns)
-     */
-    public function get_default_blocks() {
-        return array(
-            BLOCK_POS_LEFT => array(),
-            BLOCK_POS_RIGHT => array('news_items', 'recent_activity', 'calendar_upcoming')
-        );
-    }
-
-    /**
-     * Allows course format to execute code on moodle_page::set_course()
-     *
-     * If user is on course view page and there is no scorm module added to the course
-     * and the user has 'moodle/course:update' capability, redirect to create module
-     * form. This function is executed before the output starts
-     *
-     * @param moodle_page $page instance of page calling set_course
-     */
-    public function page_set_course(moodle_page $page) {
-        global $PAGE;
-        if ($PAGE == $page && $page->has_set_url() &&
-                $page->url->compare(new moodle_url('/course/view.php'), URL_MATCH_BASE)) {
-            $modinfo = get_fast_modinfo($this->courseid);
-            if (empty($modinfo->instances['scorm'])
-                    && has_capability('moodle/course:update', context_course::instance($this->courseid))) {
-                // Redirect to create a new activity
-                $url = new moodle_url('/course/modedit.php',
-                        array('course' => $this->courseid, 'section' => 0, 'add' => 'scorm'));
-                redirect($url);
-            }
-        }
-    }
-}
index e1e1240..a0b5f5a 100644 (file)
@@ -1736,35 +1736,44 @@ function reorder_sections($sections, $origin_position, $target_position) {
  * Move the module object $mod to the specified $section
  * If $beforemod exists then that is the module
  * before which $modid should be inserted
- * All parameters are objects
+ *
+ * @param stdClass|cm_info $mod
+ * @param stdClass|section_info $section
+ * @param int|stdClass $beforemod id or object with field id corresponding to the module
+ *     before which the module needs to be included. Null for inserting in the
+ *     end of the section
+ * @return int new value for module visibility (0 or 1)
  */
 function moveto_module($mod, $section, $beforemod=NULL) {
     global $OUTPUT, $DB;
 
-/// Remove original module from original section
+    // Current module visibility state - return value of this function.
+    $modvisible = $mod->visible;
+
+    // Remove original module from original section.
     if (! delete_mod_from_section($mod->id, $mod->section)) {
         echo $OUTPUT->notification("Could not delete module from existing section");
     }
 
-    // if moving to a hidden section then hide module
+    // If moving to a hidden section then hide module.
     if ($mod->section != $section->id) {
         if (!$section->visible && $mod->visible) {
-            // Set this in the object because it is sent as a response to ajax calls.
-            $mod->visible = 0;
+            // Module was visible but must become hidden after moving to hidden section.
+            $modvisible = 0;
             set_coursemodule_visible($mod->id, 0);
             // Set visibleold to 1 so module will be visible when section is made visible.
             $DB->set_field('course_modules', 'visibleold', 1, array('id' => $mod->id));
         }
         if ($section->visible && !$mod->visible) {
+            // Hidden module was moved to the visible section, restore the module visibility from visibleold.
             set_coursemodule_visible($mod->id, $mod->visibleold);
-            // Set this in the object because it is sent as a response to ajax calls.
-            $mod->visible = $mod->visibleold;
+            $modvisible = $mod->visibleold;
         }
     }
 
-/// Add the module into the new section
+    // Add the module into the new section.
     course_add_cm_to_section($section->course, $mod->id, $section->section, $beforemod);
-    return true;
+    return $modvisible;
 }
 
 /**
index 4888c0a..be8d115 100644 (file)
@@ -26,6 +26,16 @@ require_once("../config.php");
 require_once($CFG->dirroot.'/course/lib.php');
 require_once($CFG->libdir.'/coursecatlib.php');
 
+/**
+ * Limit the total number of categories where user has 'moodle/course:manage'
+ * permission in order for "Move categories" dropdown to be displayed on this page.
+ * If number of categories exceeds this limit, user can always use edit category
+ * form to change the parent. Otherwise the page size becomes too big.
+ */
+if (!defined('COURSECAT_QUICKMOVE_LIMIT')) {
+    define('COURSECAT_QUICKMOVE_LIMIT', 200);
+}
+
 // Category id.
 $id = optional_param('categoryid', 0, PARAM_INT);
 // Which page to show.
@@ -350,6 +360,8 @@ if (!empty($searchcriteria)) {
     echo $OUTPUT->heading(new lang_string('searchresults'));
 } else if (!$coursecat->id) {
     // Print out the categories with all the knobs.
+    $manageablecategories = coursecat::make_categories_list('moodle/category:manage');
+    $displaymovecategoryto = !empty($manageablecategories) && (count($manageablecategories) <= COURSECAT_QUICKMOVE_LIMIT);
     $table = new html_table;
     $table->id = 'coursecategories';
     $table->attributes['class'] = 'admintable generaltable editcourse';
@@ -357,17 +369,21 @@ if (!empty($searchcriteria)) {
         get_string('categories'),
         get_string('courses'),
         get_string('edit'),
-        get_string('movecategoryto'),
     );
+    if ($displaymovecategoryto) {
+        $table->head[] = get_string('movecategoryto');
+    }
     $table->colclasses = array(
         'leftalign name',
         'centeralign count',
         'centeralign icons',
-        'leftalign actions'
     );
+    if ($displaymovecategoryto) {
+        $table->colclasses[] = 'leftalign actions';
+    }
     $table->data = array();
 
-    print_category_edit($table, $coursecat);
+    print_category_edit($table, $coursecat, -1, false, false, $displaymovecategoryto);
 
     echo html_writer::table($table);
 } else {
@@ -655,8 +671,9 @@ echo $OUTPUT->footer();
  * @param int $depth The depth of the category.
  * @param bool $up True if this category can be moved up.
  * @param bool $down True if this category can be moved down.
+ * @param bool $displaymovecategoryto whether to display a dropdown for quick category move.
  */
-function print_category_edit(html_table $table, coursecat $category, $depth = -1, $up = false, $down = false) {
+function print_category_edit(html_table $table, coursecat $category, $depth = -1, $up = false, $down = false, $displaymovecategoryto = true) {
     global $OUTPUT;
 
     static $str = null;
@@ -744,7 +761,7 @@ function print_category_edit(html_table $table, coursecat $category, $depth = -1
         }
 
         $actions = '';
-        if (has_capability('moodle/category:manage', $categorycontext)) {
+        if ($displaymovecategoryto && has_capability('moodle/category:manage', $categorycontext)) {
             $popupurl = new moodle_url('/course/manage.php', array('movecat' => $category->id, 'sesskey' => sesskey()));
             $tempdisplaylist = array(0 => get_string('top')) + coursecat::make_categories_list('moodle/category:manage', $category->id);
             $select = new single_select($popupurl, 'movetocat', $tempdisplaylist, $category->parent, null, "moveform$category->id");
@@ -752,16 +769,19 @@ function print_category_edit(html_table $table, coursecat $category, $depth = -1
             $actions = $OUTPUT->render($select);
         }
 
-        $table->data[] = new html_table_row(array(
+        $thisrowdata = array(
             // Category name.
             new html_table_cell($categoryname),
             // Course count.
             new html_table_cell($category->coursecount),
             // Icons.
             new html_table_cell(join(' ', $icons)),
+        );
+        if ($displaymovecategoryto) {
             // Actions.
-            new html_table_cell($actions)
-        ));
+            $thisrowdata[] = new html_table_cell($actions);
+        }
+        $table->data[] = new html_table_row($thisrowdata);
     }
 
     if ($categories = $category->get_children()) {
@@ -779,7 +799,7 @@ function print_category_edit(html_table $table, coursecat $category, $depth = -1
             $down = $last ? false : true;
             $first = false;
 
-            print_category_edit($table, $cat, $depth+1, $up, $down);
+            print_category_edit($table, $cat, $depth+1, $up, $down, $displaymovecategoryto);
         }
     }
 }
index 4bae0b0..79a1152 100644 (file)
@@ -133,8 +133,8 @@ switch($requestmethod) {
                             $beforemod = NULL;
                         }
 
-                        moveto_module($cm, $section, $beforemod);
-                        echo json_encode(array('visible' => $cm->visible));
+                        $isvisible = moveto_module($cm, $section, $beforemod);
+                        echo json_encode(array('visible' => $isvisible));
                         break;
                     case 'gettitle':
                         require_capability('moodle/course:manageactivities', $modcontext);
index 01c4bfc..28837fc 100644 (file)
@@ -856,8 +856,6 @@ class core_course_courselib_testcase extends advanced_testcase {
         // Perform the move
         moveto_module($cm, $newsection);
 
-        // reset of get_fast_modinfo is usually called the code calling moveto_module so call it here
-        get_fast_modinfo(0, 0, true);
         $cms = get_fast_modinfo($course)->get_cms();
         $cm = reset($cms);
 
@@ -883,11 +881,8 @@ class core_course_courselib_testcase extends advanced_testcase {
         // Perform a second move as some issues were only seen on the second move
         $newsection = get_fast_modinfo($course)->get_section_info(2);
         $oldsectionid = $cm->section;
-        $result = moveto_module($cm, $newsection);
-        $this->assertTrue($result);
+        moveto_module($cm, $newsection);
 
-        // reset of get_fast_modinfo is usually called the code calling moveto_module so call it here
-        get_fast_modinfo(0, 0, true);
         $cms = get_fast_modinfo($course)->get_cms();
         $cm = reset($cms);
 
@@ -1197,12 +1192,9 @@ class core_course_courselib_testcase extends advanced_testcase {
         $forumcm = $modinfo->cms[$forum->cmid];
         $pagecm = $modinfo->cms[$page->cmid];
 
-        // Move the forum and the page to a hidden section.
-        moveto_module($forumcm, $hiddensection);
-        moveto_module($pagecm, $hiddensection);
-
-        // Reset modinfo cache.
-        get_fast_modinfo(0, 0, true);
+        // Move the forum and the page to a hidden section, make sure moveto_module returns 0 as new visibility state.
+        $this->assertEquals(0, moveto_module($forumcm, $hiddensection));
+        $this->assertEquals(0, moveto_module($pagecm, $hiddensection));
 
         $modinfo = get_fast_modinfo($course);
 
@@ -1228,29 +1220,26 @@ class core_course_courselib_testcase extends advanced_testcase {
         $this->assertEquals($quizcm->visible, 1);
 
         // Move forum and page back to visible section.
+        // Make sure the visibility is restored to the original value (visible for forum and hidden for page).
         $visiblesection = $modinfo->get_section_info(2);
-        moveto_module($forumcm, $visiblesection);
-        moveto_module($pagecm, $visiblesection);
+        $this->assertEquals(1, moveto_module($forumcm, $visiblesection));
+        $this->assertEquals(0, moveto_module($pagecm, $visiblesection));
 
-        // Reset modinfo cache.
-        get_fast_modinfo(0, 0, true);
         $modinfo = get_fast_modinfo($course);
 
-        // Verify that forum has been made visible.
+        // Double check that forum has been made visible.
         $forumcm = $modinfo->cms[$forum->cmid];
         $this->assertEquals($forumcm->visible, 1);
 
-        // Verify that page has stayed invisible.
+        // Double check that page has stayed invisible.
         $pagecm = $modinfo->cms[$page->cmid];
         $this->assertEquals($pagecm->visible, 0);
 
-        // Move the page in the same section (this is what mod duplicate does_
-        moveto_module($pagecm, $visiblesection, $forumcm);
+        // Move the page in the same section (this is what mod duplicate does).
+        // Visibility of page remains 0.
+        $this->assertEquals(0, moveto_module($pagecm, $visiblesection, $forumcm));
 
-        // Reset modinfo cache.
-        get_fast_modinfo(0, 0, true);
-
-        // Verify that the the page is still hidden
+        // Double check that the the page is still hidden.
         $modinfo = get_fast_modinfo($course);
         $pagecm = $modinfo->cms[$page->cmid];
         $this->assertEquals($pagecm->visible, 0);
@@ -1288,13 +1277,10 @@ class core_course_courselib_testcase extends advanced_testcase {
         $this->assertEquals($section->id, $pagecm->section);
 
 
-        // Move the forum and the page to a hidden section.
-        moveto_module($pagecm, $section, $forumcm);
-
-        // Reset modinfo cache.
-        get_fast_modinfo(0, 0, true);
+        // Move the page inside the hidden section. Make sure it is hidden.
+        $this->assertEquals(0, moveto_module($pagecm, $section, $forumcm));
 
-        // Verify that the the page is still hidden
+        // Double check that the the page is still hidden.
         $modinfo = get_fast_modinfo($course);
         $pagecm = $modinfo->cms[$page->cmid];
         $this->assertEquals($pagecm->visible, 0);
index cf3748a..5430b6c 100644 (file)
         redirect($CFG->wwwroot .'/');
     }
 
+    $ajaxenabled = ajaxenabled();
+
     $completion = new completion_info($course);
-    if ($completion->is_enabled() && ajaxenabled()) {
+    if ($completion->is_enabled() && $ajaxenabled) {
         $PAGE->requires->string_for_js('completion-title-manual-y', 'completion');
         $PAGE->requires->string_for_js('completion-title-manual-n', 'completion');
         $PAGE->requires->string_for_js('completion-alt-manual-y', 'completion');
     $PAGE->set_heading($course->fullname);
     echo $OUTPUT->header();
 
-    if ($completion->is_enabled() && ajaxenabled()) {
+    if ($completion->is_enabled() && $ajaxenabled) {
         // This value tracks whether there has been a dynamic change to the page.
         // It is used so that if a user does this - (a) set some tickmarks, (b)
         // go to another page, (c) clicks Back button - the page will
index f6ec711..4d85032 100644 (file)
@@ -53,6 +53,7 @@ $string['course_summary_key'] = 'Summary';
 $string['createcourseextid'] = 'CREATE User enrolled to a nonexistant course \'{$a->courseextid}\'';
 $string['createnotcourseextid'] = 'User enrolled to a nonexistant course \'{$a->courseextid}\'';
 $string['creatingcourse'] =  'Creating course \'{$a}\'...';
+$string['duplicateshortname'] = "Course creation failed. Duplicate short name. Skipping course with idnumber '{\$a->idnumber}'...";
 $string['editlock'] = 'Lock value';
 $string['emptyenrolment'] = "Empty enrolment for role '{\$a->role_shortname}' in course '{\$a->course_shortname}'\n";
 $string['enrolname'] = 'LDAP';
index 9ebacb8..73eb118 100644 (file)
@@ -990,6 +990,12 @@ class enrol_ldap_plugin extends enrol_plugin {
             $course->summary = $course_ext[$this->get_config('course_summary')][0];
         }
 
+        // Check if the shortname already exists if it does - skip course creation.
+        if ($DB->record_exists('course', array('shortname' => $course->shortname))) {
+            $trace->output(get_string('duplicateshortname', 'enrol_ldap', $course));
+            return false;
+        }
+
         $newcourse = create_course($course);
         return $newcourse->id;
     }
index 5c3e345..a4f7a00 100644 (file)
@@ -257,7 +257,17 @@ class enrol_manual_editselectedusers_operation extends enrol_bulk_enrolment_oper
                 foreach ($user->enrolments as $enrolment) {
                     $enrolment->courseid  = $enrolment->enrolmentinstance->courseid;
                     $enrolment->enrol     = 'manual';
-                    events_trigger('user_enrol_modified', $enrolment);
+                    // Trigger event.
+                    $event = \core\event\user_enrolment_updated::create(
+                            array(
+                                'objectid' => $enrolment->id,
+                                'courseid' => $enrolment->courseid,
+                                'context' => context_course::instance($enrolment->courseid),
+                                'relateduserid' => $user->id,
+                                'other' => array('enrol' => 'manual')
+                                )
+                            );
+                    $event->trigger();
                 }
             }
             return true;
diff --git a/enrol/meta/classes/observer.php b/enrol/meta/classes/observer.php
new file mode 100644 (file)
index 0000000..625d232
--- /dev/null
@@ -0,0 +1,102 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Event observer for meta enrolment plugin.
+ *
+ * @package    enrol_meta
+ * @copyright  2013 Rajesh Taneja <rajesh@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+defined('MOODLE_INTERNAL') || die();
+
+require_once($CFG->dirroot.'/enrol/meta/locallib.php');
+
+/**
+ * Event observer for enrol_meta.
+ *
+ * @package    enrol_meta
+ * @copyright  2013 Rajesh Taneja <rajesh@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class enrol_meta_observer extends enrol_meta_handler {
+
+    /**
+     * Triggered via user_enrolment_created event.
+     *
+     * @param \core\event\user_enrolment_created $event
+     * @return bool true on success.
+     */
+    public static function user_enrolment_created(\core\event\user_enrolment_created $event) {
+        if (!enrol_is_enabled('meta')) {
+            // No more enrolments for disabled plugins.
+            return true;
+        }
+
+        if ($event->other['enrol'] === 'meta') {
+            // Prevent circular dependencies - we can not sync meta enrolments recursively.
+            return true;
+        }
+
+        self::sync_course_instances($event->courseid, $event->relateduserid);
+        return true;
+    }
+
+    /**
+     * Triggered via user_enrolment_deleted event.
+     *
+     * @param \core\event\user_enrolment_deleted $event
+     * @return bool true on success.
+     */
+    public static function user_enrolment_deleted(\core\event\user_enrolment_deleted $event) {
+        if (!enrol_is_enabled('meta')) {
+            // This is slow, let enrol_meta_sync() deal with disabled plugin.
+            return true;
+        }
+
+        if ($event->other['enrol'] === 'meta') {
+            // Prevent circular dependencies - we can not sync meta enrolments recursively.
+            return true;
+        }
+
+        self::sync_course_instances($event->courseid, $event->relateduserid);
+
+        return true;
+    }
+
+    /**
+     * Triggered via user_enrolment_updated event.
+     *
+     * @param \core\event\user_enrolment_updated $event
+     * @return bool true on success
+     */
+    public static function user_enrolment_updated(\core\event\user_enrolment_updated $event) {
+        if (!enrol_is_enabled('meta')) {
+            // No modifications if plugin disabled.
+            return true;
+        }
+
+        if ($event->other['enrol'] === 'meta') {
+            // Prevent circular dependencies - we can not sync meta enrolments recursively.
+            return true;
+        }
+
+        self::sync_course_instances($event->courseid, $event->relateduserid);
+
+        return true;
+    }
+}
index c1a2d70..08c9625 100644 (file)
@@ -41,31 +41,27 @@ $handlers = array (
         'internal'         => 1,
     ),
 
-    'user_enrolled' => array (
+    'course_deleted' => array (
         'handlerfile'      => '/enrol/meta/locallib.php',
-        'handlerfunction'  => array('enrol_meta_handler', 'user_enrolled'),
+        'handlerfunction'  => array('enrol_meta_handler', 'course_deleted'),
         'schedule'         => 'instant',
         'internal'         => 1,
     ),
+);
 
-    'user_unenrolled' => array (
-        'handlerfile'      => '/enrol/meta/locallib.php',
-        'handlerfunction'  => array('enrol_meta_handler', 'user_unenrolled'),
-        'schedule'         => 'instant',
-        'internal'         => 1,
-    ),
+// List of observers.
+$observers = array(
 
-    'user_enrol_modified' => array (
-        'handlerfile'      => '/enrol/meta/locallib.php',
-        'handlerfunction'  => array('enrol_meta_handler', 'user_enrol_modified'),
-        'schedule'         => 'instant',
-        'internal'         => 1,
+    array(
+        'eventname'   => '\core\event\user_enrolment_created',
+        'callback'    => 'enrol_meta_observer::user_enrolment_created',
     ),
-
-    'course_deleted' => array (
-        'handlerfile'      => '/enrol/meta/locallib.php',
-        'handlerfunction'  => array('enrol_meta_handler', 'course_deleted'),
-        'schedule'         => 'instant',
-        'internal'         => 1,
+    array(
+        'eventname'   => '\core\event\user_enrolment_deleted',
+        'callback'    => 'enrol_meta_observer::user_enrolment_deleted',
+    ),
+    array(
+        'eventname'   => '\core\event\user_enrolment_updated',
+        'callback'    => 'enrol_meta_observer::user_enrolment_updated',
     ),
 );
index 3a3c26b..c20bfaf 100644 (file)
@@ -293,72 +293,6 @@ class enrol_meta_handler {
         return true;
     }
 
-    /**
-     * Triggered via user enrolled event.
-     * @static
-     * @param stdClass $ue
-     * @return bool success
-     */
-    public static function user_enrolled($ue) {
-        if (!enrol_is_enabled('meta')) {
-            // no more enrolments for disabled plugins
-            return true;
-        }
-
-        if ($ue->enrol === 'meta') {
-            // prevent circular dependencies - we can not sync meta enrolments recursively
-            return true;
-        }
-
-        self::sync_course_instances($ue->courseid, $ue->userid);
-
-        return true;
-    }
-
-    /**
-     * Triggered via user unenrolled event.
-     * @static
-     * @param stdClass $ue
-     * @return bool success
-     */
-    public static function user_unenrolled($ue) {
-        if (!enrol_is_enabled('meta')) {
-            // This is slow, let enrol_meta_sync() deal with disabled plugin.
-            return true;
-        }
-
-        if ($ue->enrol === 'meta') {
-            // prevent circular dependencies - we can not sync meta enrolments recursively
-            return true;
-        }
-
-        self::sync_course_instances($ue->courseid, $ue->userid);
-
-        return true;
-    }
-
-    /**
-     * Triggered via user enrolment modification  event.
-     * @static
-     * @param stdClass $ue
-     * @return bool success
-     */
-    public static function user_enrol_modified($ue) {
-        if (!enrol_is_enabled('meta')) {
-            // no modifications if plugin disabled
-            return true;
-        }
-
-        if ($ue->enrol === 'meta') {
-            // prevent circular dependencies - we can not sync meta enrolments recursively
-            return true;
-        }
-
-        self::sync_course_instances($ue->courseid, $ue->userid);
-
-        return true;
-    }
-
     /**
      * Triggered via course_deleted event.
      * @static
index fdfec33..05cb522 100644 (file)
@@ -439,4 +439,112 @@ class enrol_meta_plugin_testcase extends advanced_testcase {
         delete_course($course4, false);
 
     }
+
+    /**
+     * Test user_enrolment_created event.
+     */
+    public function test_user_enrolment_created_observer() {
+        global $DB;
+
+        $this->resetAfterTest();
+
+        $metaplugin = enrol_get_plugin('meta');
+        $user1 = $this->getDataGenerator()->create_user();
+        $course1 = $this->getDataGenerator()->create_course();
+        $course2 = $this->getDataGenerator()->create_course();
+        $student = $DB->get_record('role', array('shortname' => 'student'));
+
+        $e1 = $metaplugin->add_instance($course2, array('customint1' => $course1->id));
+        $enrol1 = $DB->get_record('enrol', array('id' => $e1));
+
+        // Enrol user and capture event.
+        $sink = $this->redirectEvents();
+
+        $metaplugin->enrol_user($enrol1, $user1->id, $student->id);
+        $events = $sink->get_events();
+        $sink->close();
+        $event = array_shift($events);
+
+        // Test Event.
+        $dbuserenrolled = $DB->get_record('user_enrolments', array('userid' => $user1->id));
+        $this->assertInstanceOf('\core\event\user_enrolment_created', $event);
+        $this->assertEquals($dbuserenrolled->id, $event->objectid);
+        $this->assertEquals('user_enrolled', $event->get_legacy_eventname());
+        $expectedlegacyeventdata = $dbuserenrolled;
+        $expectedlegacyeventdata->enrol = 'meta';
+        $expectedlegacyeventdata->courseid = $course2->id;
+        $this->assertEventLegacyData($expectedlegacyeventdata, $event);
+    }
+
+    /**
+     * Test user_enrolment_deleted observer.
+     */
+    public function test_user_enrolment_deleted_observer() {
+        global $DB;
+
+        $this->resetAfterTest(true);
+
+        $metalplugin = enrol_get_plugin('meta');
+        $user1 = $this->getDataGenerator()->create_user();
+        $course1 = $this->getDataGenerator()->create_course();
+        $course2 = $this->getDataGenerator()->create_course();
+        $student = $DB->get_record('role', array('shortname'=>'student'));
+
+        $e1 = $metalplugin->add_instance($course2, array('customint1' => $course1->id));
+        $enrol1 = $DB->get_record('enrol', array('id' => $e1));
+
+        // Enrol user.
+        $metalplugin->enrol_user($enrol1, $user1->id, $student->id);
+        $this->assertEquals(1, $DB->count_records('user_enrolments'));
+
+        // Unenrol user and capture event.
+        $sink = $this->redirectEvents();
+        $metalplugin->unenrol_user($enrol1, $user1->id);
+        $events = $sink->get_events();
+        $sink->close();
+        $event = array_pop($events);
+
+        $this->assertEquals(0, $DB->count_records('user_enrolments'));
+        $this->assertInstanceOf('\core\event\user_enrolment_deleted', $event);
+        $this->assertEquals('user_unenrolled', $event->get_legacy_eventname());
+    }
+
+    /**
+     * Test user_enrolment_updated event.
+     */
+    public function test_user_enrolment_updated_observer() {
+        global $DB;
+
+        $this->resetAfterTest(true);
+
+        $metalplugin = enrol_get_plugin('meta');
+        $user1 = $this->getDataGenerator()->create_user();
+        $course1 = $this->getDataGenerator()->create_course();
+        $course2 = $this->getDataGenerator()->create_course();
+        $student = $DB->get_record('role', array('shortname'=>'student'));
+
+        $e1 = $metalplugin->add_instance($course2, array('customint1' => $course1->id));
+        $enrol1 = $DB->get_record('enrol', array('id' => $e1));
+
+        // Enrol user.
+        $metalplugin->enrol_user($enrol1, $user1->id, $student->id);
+        $this->assertEquals(1, $DB->count_records('user_enrolments'));
+
+        // Updated enrolment for user and capture event.
+        $sink = $this->redirectEvents();
+        $metalplugin->update_user_enrol($enrol1, $user1->id, ENROL_USER_SUSPENDED, null, time());
+        $events = $sink->get_events();
+        $sink->close();
+        $event = array_shift($events);
+
+        // Test Event.
+        $dbuserenrolled = $DB->get_record('user_enrolments', array('userid' => $user1->id));
+        $this->assertInstanceOf('\core\event\user_enrolment_updated', $event);
+        $this->assertEquals($dbuserenrolled->id, $event->objectid);
+        $this->assertEquals('user_enrol_modified', $event->get_legacy_eventname());
+        $expectedlegacyeventdata = $dbuserenrolled;
+        $expectedlegacyeventdata->enrol = 'meta';
+        $expectedlegacyeventdata->courseid = $course2->id;
+        $this->assertEventLegacyData($expectedlegacyeventdata, $event);
+    }
 }
index cba40a8..bbdc3bc 100644 (file)
@@ -279,4 +279,41 @@ class core_enrollib_testcase extends advanced_testcase {
         // It should be course 1.
         $this->assertEquals($sharedcourse->id, $course1->id);
     }
+
+    /**
+     * Test user enrolment created event.
+     */
+    public function test_user_enrolment_created_event() {
+        global $DB;
+
+        $this->resetAfterTest();
+
+        $studentrole = $DB->get_record('role', array('shortname'=>'student'));
+        $this->assertNotEmpty($studentrole);
+
+        $admin = get_admin();
+
+        $course1 = $this->getDataGenerator()->create_course();
+
+        $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
+
+        $manual = enrol_get_plugin('manual');
+        $this->assertNotEmpty($manual);
+
+        // Enrol user and capture event.
+        $sink = $this->redirectEvents();
+        $manual->enrol_user($maninstance1, $admin->id, $studentrole->id);
+        $events = $sink->get_events();
+        $sink->close();
+        $event = array_shift($events);
+
+        $dbuserenrolled = $DB->get_record('user_enrolments', array('userid' => $admin->id));
+        $this->assertInstanceOf('\core\event\user_enrolment_created', $event);
+        $this->assertEquals($dbuserenrolled->id, $event->objectid);
+        $this->assertEquals('user_enrolled', $event->get_legacy_eventname());
+        $expectedlegacyeventdata = $dbuserenrolled;
+        $expectedlegacyeventdata->enrol = $manual->get_name();
+        $expectedlegacyeventdata->courseid = $course1->id;
+        $this->assertEventLegacyData($expectedlegacyeventdata, $event);
+    }
 }
index 199d1eb..8ccb757 100644 (file)
@@ -201,9 +201,9 @@ class core_files_renderer extends plugin_renderer_base {
     <div class="fp-navbar">
         <div class="filemanager-toolbar">
             <div class="fp-toolbar">
-                <div class="{!}fp-btn-add"><a href="#"><img src="'.$this->pix_url('a/add_file').'" /> '.$straddfile.'</a></div>
-                <div class="{!}fp-btn-mkdir"><a href="#"><img src="'.$this->pix_url('a/create_folder').'" /> '.$strmakedir.'</a></div>
-                <div class="{!}fp-btn-download"><a href="#"><img src="'.$this->pix_url('a/download_all').'" /> '.$strdownload.'</a></div>
+                <div class="{!}fp-btn-add"><a role="button" href="#"><img src="'.$this->pix_url('a/add_file').'" /> '.$straddfile.'</a></div>
+                <div class="{!}fp-btn-mkdir"><a role="button" href="#"><img src="'.$this->pix_url('a/create_folder').'" /> '.$strmakedir.'</a></div>
+                <div class="{!}fp-btn-download"><a role="button" href="#"><img src="'.$this->pix_url('a/download_all').'" /> '.$strdownload.'</a></div>
             </div>
             <div class="{!}fp-viewbar">
                 <a title="'. get_string('displayicons', 'repository') .'" class="{!}fp-vb-icons" href="#"></a>
@@ -521,10 +521,10 @@ class core_files_renderer extends plugin_renderer_base {
      */
     private function fp_js_template_generallayout() {