MDL-49231 mod_glossary: External function view_glossary
This commit is contained in:
committed by
Juan Leyva
parent
ef343c3299
commit
d0d4372ce7
@@ -1277,6 +1277,7 @@ $services = array(
|
||||
'mod_wiki_get_wikis_by_courses',
|
||||
'mod_wiki_view_wiki',
|
||||
'mod_wiki_view_page',
|
||||
'mod_glossary_view_glossary',
|
||||
),
|
||||
'enabled' => 0,
|
||||
'restrictedusers' => 0,
|
||||
|
||||
@@ -23,8 +23,11 @@
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @since Moodle 3.1
|
||||
*/
|
||||
defined('MOODLE_INTERNAL') || die;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
require_once($CFG->libdir . '/externallib.php');
|
||||
require_once($CFG->dirroot . '/mod/glossary/lib.php');
|
||||
|
||||
/**
|
||||
* Glossary module external functions.
|
||||
@@ -157,4 +160,65 @@ class mod_glossary_external extends external_api {
|
||||
'warnings' => new external_warnings())
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the description of the external function parameters.
|
||||
*
|
||||
* @return external_function_parameters
|
||||
* @since Moodle 3.1
|
||||
*/
|
||||
public static function view_glossary_parameters() {
|
||||
return new external_function_parameters(array(
|
||||
'id' => new external_value(PARAM_INT, 'Glossary instance ID'),
|
||||
'mode' => new external_value(PARAM_ALPHA, 'The mode in which the glossary is viewed'),
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Notify that the course module was viewed.
|
||||
*
|
||||
* @param int $id The glossary instance ID.
|
||||
* @return array of warnings and status result
|
||||
* @since Moodle 3.1
|
||||
* @throws moodle_exception
|
||||
*/
|
||||
public static function view_glossary($id, $mode) {
|
||||
global $DB;
|
||||
|
||||
$params = self::validate_parameters(self::view_glossary_parameters(), array(
|
||||
'id' => $id,
|
||||
'mode' => $mode
|
||||
));
|
||||
$id = $params['id'];
|
||||
$mode = $params['mode'];
|
||||
$warnings = array();
|
||||
|
||||
// Fetch and confirm.
|
||||
$glossary = $DB->get_record('glossary', array('id' => $id), '*', MUST_EXIST);
|
||||
list($course, $cm) = get_course_and_cm_from_instance($glossary, 'glossary');
|
||||
$context = context_module::instance($cm->id);
|
||||
self::validate_context($context);
|
||||
|
||||
// Trigger module viewed event.
|
||||
glossary_view($glossary, $course, $cm, $context, $mode);
|
||||
|
||||
return array(
|
||||
'status' => true,
|
||||
'warnings' => $warnings
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the description of the external function return value.
|
||||
*
|
||||
* @return external_description
|
||||
* @since Moodle 3.1
|
||||
*/
|
||||
public static function view_glossary_returns() {
|
||||
return new external_single_structure(array(
|
||||
'status' => new external_value(PARAM_BOOL, 'True on success'),
|
||||
'warnings' => new external_warnings()
|
||||
));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -35,4 +35,12 @@ $functions = array(
|
||||
'capabilities' => 'mod/glossary:view'
|
||||
),
|
||||
|
||||
'mod_glossary_view_glossary' => array(
|
||||
'classname' => 'mod_glossary_external',
|
||||
'methodname' => 'view_glossary',
|
||||
'description' => 'Notify the glossary as being viewed.',
|
||||
'type' => 'write',
|
||||
'capabilities' => 'mod/glossary:view'
|
||||
),
|
||||
|
||||
);
|
||||
|
||||
@@ -3295,3 +3295,34 @@ function glossary_get_visible_tabs($displayformat) {
|
||||
|
||||
return $showtabs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Notify that the glossary was viewed.
|
||||
*
|
||||
* This will trigger relevant events and activity completion.
|
||||
*
|
||||
* @param stdClass $glossary The glossary object.
|
||||
* @param stdClass $course The course object.
|
||||
* @param stdClass $cm The course module object.
|
||||
* @param stdClass $context The context object.
|
||||
* @param string $mode The mode in which the glossary was viewed.
|
||||
* @since Moodle 3.1
|
||||
*/
|
||||
function glossary_view($glossary, $course, $cm, $context, $mode) {
|
||||
|
||||
// Completion trigger.
|
||||
$completion = new completion_info($course);
|
||||
$completion->set_module_viewed($cm);
|
||||
|
||||
// Trigger the course module viewed event.
|
||||
$event = \mod_glossary\event\course_module_viewed::create(array(
|
||||
'objectid' => $glossary->id,
|
||||
'context' => $context,
|
||||
'other' => array('mode' => $mode)
|
||||
));
|
||||
$event->add_record_snapshot('course', $course);
|
||||
$event->add_record_snapshot('course_modules', $cm);
|
||||
$event->add_record_snapshot('glossary', $glossary);
|
||||
$event->trigger();
|
||||
}
|
||||
|
||||
|
||||
@@ -85,4 +85,49 @@ class mod_glossary_external_testcase extends externallib_advanced_testcase {
|
||||
$this->assertEquals('Third Glossary', $glossaries['glossaries'][0]['name']);
|
||||
}
|
||||
|
||||
public function test_view_glossary() {
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
// Generate all the things.
|
||||
$c1 = $this->getDataGenerator()->create_course();
|
||||
$g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
|
||||
$u1 = $this->getDataGenerator()->create_user();
|
||||
$this->getDataGenerator()->enrol_user($u1->id, $c1->id);
|
||||
|
||||
$sink = $this->redirectEvents();
|
||||
$this->setUser($u1);
|
||||
$return = mod_glossary_external::view_glossary($g1->id, 'letter');
|
||||
$return = external_api::clean_returnvalue(mod_glossary_external::view_glossary_returns(), $return);
|
||||
$events = $sink->get_events();
|
||||
|
||||
// Assertion.
|
||||
$this->assertTrue($return['status']);
|
||||
$this->assertEmpty($return['warnings']);
|
||||
$this->assertCount(1, $events);
|
||||
$this->assertEquals('\mod_glossary\event\course_module_viewed', $events[0]->eventname);
|
||||
$sink->close();
|
||||
}
|
||||
|
||||
public function test_view_glossary_without_permission() {
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
// Generate all the things.
|
||||
$c1 = $this->getDataGenerator()->create_course();
|
||||
$g1 = $this->getDataGenerator()->create_module('glossary', array('course' => $c1->id));
|
||||
$u1 = $this->getDataGenerator()->create_user();
|
||||
$this->getDataGenerator()->enrol_user($u1->id, $c1->id);
|
||||
$ctx = context_module::instance($g1->cmid);
|
||||
|
||||
// Revoke permission.
|
||||
$roles = get_archetype_roles('user');
|
||||
$role = array_shift($roles);
|
||||
assign_capability('mod/glossary:view', CAP_PROHIBIT, $role->id, $ctx, true);
|
||||
accesslib_clear_all_caches_for_unit_testing();
|
||||
|
||||
// Assertion.
|
||||
$this->setUser($u1);
|
||||
$this->setExpectedException('require_login_exception', 'Activity is hidden');
|
||||
mod_glossary_external::view_glossary($g1->id, 'letter');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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/>.
|
||||
|
||||
/**
|
||||
* Glossary lib tests.
|
||||
*
|
||||
* @package mod_glossary
|
||||
* @copyright 2015 Frédéric Massart - FMCorz.net
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
global $CFG;
|
||||
require_once($CFG->dirroot . '/mod/glossary/lib.php');
|
||||
|
||||
/**
|
||||
* Glossary lib testcase.
|
||||
*
|
||||
* @package mod_glossary
|
||||
* @copyright 2015 Frédéric Massart - FMCorz.net
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class mod_glossary_lib_testcase extends advanced_testcase {
|
||||
|
||||
public function test_glossary_view() {
|
||||
global $CFG;
|
||||
$origcompletion = $CFG->enablecompletion;
|
||||
$CFG->enablecompletion = true;
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
// Generate all the things.
|
||||
$c1 = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
|
||||
$g1 = $this->getDataGenerator()->create_module('glossary', array(
|
||||
'course' => $c1->id,
|
||||
'completion' => COMPLETION_TRACKING_AUTOMATIC,
|
||||
'completionview' => 1
|
||||
));
|
||||
$g2 = $this->getDataGenerator()->create_module('glossary', array(
|
||||
'course' => $c1->id,
|
||||
'completion' => COMPLETION_TRACKING_AUTOMATIC,
|
||||
'completionview' => 1
|
||||
));
|
||||
$u1 = $this->getDataGenerator()->create_user();
|
||||
$this->getDataGenerator()->enrol_user($u1->id, $c1->id);
|
||||
$modinfo = course_modinfo::instance($c1->id);
|
||||
$cm1 = $modinfo->get_cm($g1->cmid);
|
||||
$cm2 = $modinfo->get_cm($g2->cmid);
|
||||
$ctx1 = $cm1->context;
|
||||
$completion = new completion_info($c1);
|
||||
|
||||
$this->setUser($u1);
|
||||
|
||||
// Confirm what we've set up.
|
||||
$this->assertEquals(COMPLETION_NOT_VIEWED, $completion->get_data($cm1, false, $u1->id)->viewed);
|
||||
$this->assertEquals(COMPLETION_INCOMPLETE, $completion->get_data($cm1, false, $u1->id)->completionstate);
|
||||
$this->assertEquals(COMPLETION_NOT_VIEWED, $completion->get_data($cm2, false, $u1->id)->viewed);
|
||||
$this->assertEquals(COMPLETION_INCOMPLETE, $completion->get_data($cm2, false, $u1->id)->completionstate);
|
||||
|
||||
// Simulate the view call.
|
||||
$sink = $this->redirectEvents();
|
||||
glossary_view($g1, $c1, $cm1, $ctx1, 'letter');
|
||||
$events = $sink->get_events();
|
||||
|
||||
// Assertions.
|
||||
$this->assertCount(3, $events);
|
||||
$this->assertEquals('\core\event\course_module_completion_updated', $events[0]->eventname);
|
||||
$this->assertEquals('\core\event\course_module_completion_updated', $events[1]->eventname);
|
||||
$this->assertEquals('\mod_glossary\event\course_module_viewed', $events[2]->eventname);
|
||||
$this->assertEquals($g1->id, $events[2]->objectid);
|
||||
$this->assertEquals('letter', $events[2]->other['mode']);
|
||||
$this->assertEquals(COMPLETION_VIEWED, $completion->get_data($cm1, false, $u1->id)->viewed);
|
||||
$this->assertEquals(COMPLETION_COMPLETE, $completion->get_data($cm1, false, $u1->id)->completionstate);
|
||||
$this->assertEquals(COMPLETION_NOT_VIEWED, $completion->get_data($cm2, false, $u1->id)->viewed);
|
||||
$this->assertEquals(COMPLETION_INCOMPLETE, $completion->get_data($cm2, false, $u1->id)->completionstate);
|
||||
|
||||
// Tear down.
|
||||
$sink->close();
|
||||
$CFG->enablecompletion = $origcompletion;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -24,7 +24,7 @@
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
$plugin->version = 2015111601; // The current module version (Date: YYYYMMDDXX)
|
||||
$plugin->version = 2015111602; // The current module version (Date: YYYYMMDDXX)
|
||||
$plugin->requires = 2015111000; // Requires this Moodle version
|
||||
$plugin->component = 'mod_glossary'; // Full name of the plugin (used for diagnostics)
|
||||
$plugin->cron = 0;
|
||||
|
||||
+1
-13
@@ -255,19 +255,7 @@ break;
|
||||
}
|
||||
|
||||
// Trigger module viewed event.
|
||||
$event = \mod_glossary\event\course_module_viewed::create(array(
|
||||
'objectid' => $glossary->id,
|
||||
'context' => $context,
|
||||
'other' => array('mode' => $mode)
|
||||
));
|
||||
$event->add_record_snapshot('course', $course);
|
||||
$event->add_record_snapshot('course_modules', $cm);
|
||||
$event->add_record_snapshot('glossary', $glossary);
|
||||
$event->trigger();
|
||||
|
||||
// Mark as viewed
|
||||
$completion = new completion_info($course);
|
||||
$completion->set_module_viewed($cm);
|
||||
glossary_view($glossary, $course, $cm, $context, $mode);
|
||||
|
||||
/// Printing the heading
|
||||
$strglossaries = get_string("modulenameplural", "glossary");
|
||||
|
||||
+1
-1
@@ -29,7 +29,7 @@
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
$version = 2015123100.00; // YYYYMMDD = weekly release date of this DEV branch.
|
||||
$version = 2015123100.01; // YYYYMMDD = weekly release date of this DEV branch.
|
||||
// RR = release increments - 00 in DEV branches.
|
||||
// .XX = incremental changes.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user