Commit 1f773493 authored by Juan Leyva's avatar Juan Leyva Committed by Eloy Lafuente
Browse files

MDL-50538 mod_feedback: New WS mod_feedback_get_feedbacks_by_courses

parent 732bd131
<?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/>.
/**
* Feedback external API
*
* @package mod_feedback
* @category external
* @copyright 2017 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.3
*/
defined('MOODLE_INTERNAL') || die;
require_once("$CFG->libdir/externallib.php");
use mod_feedback\external\feedback_summary_exporter;
/**
* Feedback external functions
*
* @package mod_feedback
* @category external
* @copyright 2017 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.3
*/
class mod_feedback_external extends external_api {
/**
* Describes the parameters for get_feedbacks_by_courses.
*
* @return external_function_parameters
* @since Moodle 3.3
*/
public static function get_feedbacks_by_courses_parameters() {
return new external_function_parameters (
array(
'courseids' => new external_multiple_structure(
new external_value(PARAM_INT, 'Course id'), 'Array of course ids', VALUE_DEFAULT, array()
),
)
);
}
/**
* Returns a list of feedbacks in a provided list of courses.
* If no list is provided all feedbacks that the user can view will be returned.
*
* @param array $courseids course ids
* @return array of warnings and feedbacks
* @since Moodle 3.3
*/
public static function get_feedbacks_by_courses($courseids = array()) {
global $PAGE;
$warnings = array();
$returnedfeedbacks = array();
$params = array(
'courseids' => $courseids,
);
$params = self::validate_parameters(self::get_feedbacks_by_courses_parameters(), $params);
$mycourses = array();
if (empty($params['courseids'])) {
$mycourses = enrol_get_my_courses();
$params['courseids'] = array_keys($mycourses);
}
// Ensure there are courseids to loop through.
if (!empty($params['courseids'])) {
list($courses, $warnings) = external_util::validate_courses($params['courseids'], $mycourses);
$output = $PAGE->get_renderer('core');
// Get the feedbacks in this course, this function checks users visibility permissions.
// We can avoid then additional validate_context calls.
$feedbacks = get_all_instances_in_courses("feedback", $courses);
foreach ($feedbacks as $feedback) {
$context = context_module::instance($feedback->coursemodule);
// Remove fields that are not from the feedback (added by get_all_instances_in_courses).
unset($feedback->coursemodule, $feedback->context, $feedback->visible, $feedback->section, $feedback->groupmode,
$feedback->groupingid);
// Check permissions.
if (!has_capability('mod/feedback:edititems', $context)) {
// Don't return the optional properties.
$properties = feedback_summary_exporter::properties_definition();
foreach ($properties as $property => $config) {
if (!empty($config['optional'])) {
unset($feedback->{$property});
}
}
}
$exporter = new feedback_summary_exporter($feedback, array('context' => $context));
$returnedfeedbacks[] = $exporter->export($output);
}
}
$result = array(
'feedbacks' => $returnedfeedbacks,
'warnings' => $warnings
);
return $result;
}
/**
* Describes the get_feedbacks_by_courses return value.
*
* @return external_single_structure
* @since Moodle 3.3
*/
public static function get_feedbacks_by_courses_returns() {
return new external_single_structure(
array(
'feedbacks' => new external_multiple_structure(
feedback_summary_exporter::get_read_structure()
),
'warnings' => new external_warnings(),
)
);
}
}
<?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/>.
/**
* Class for exporting partial feedback data.
*
* @package mod_feedback
* @copyright 2017 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_feedback\external;
defined('MOODLE_INTERNAL') || die();
use core\external\exporter;
use renderer_base;
use external_util;
use external_files;
/**
* Class for exporting partial feedback data (some fields are only viewable by admins).
*
* @copyright 2017 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class feedback_summary_exporter extends exporter {
protected static function define_properties() {
return array(
'id' => array(
'type' => PARAM_INT,
'description' => 'The primary key of the record.',
),
'course' => array(
'type' => PARAM_INT,
'description' => 'Course id this feedback is part of.',
),
'name' => array(
'type' => PARAM_TEXT,
'description' => 'Feedback name.',
),
'intro' => array(
'default' => '',
'type' => PARAM_RAW,
'description' => 'Feedback introduction text.',
),
'introformat' => array(
'choices' => array(FORMAT_HTML, FORMAT_MOODLE, FORMAT_PLAIN, FORMAT_MARKDOWN),
'type' => PARAM_INT,
'default' => FORMAT_MOODLE,
'description' => 'Feedback intro text format.',
),
'anonymous' => array(
'type' => PARAM_INT,
'description' => 'Whether the feedback is anonymous.',
),
'email_notification' => array(
'type' => PARAM_BOOL,
'optional' => true,
'description' => 'Whether email notifications will be sent to teachers.',
),
'multiple_submit' => array(
'default' => 1,
'type' => PARAM_BOOL,
'description' => 'Whether multiple submissions are allowed.',
),
'autonumbering' => array(
'default' => 1,
'type' => PARAM_BOOL,
'description' => 'Whether questions should be auto-numbered.',
),
'site_after_submit' => array(
'type' => PARAM_TEXT,
'optional' => true,
'description' => 'Link to next page after submission.',
),
'page_after_submit' => array(
'type' => PARAM_RAW,
'optional' => true,
'description' => 'Text to display after submission.',
),
'page_after_submitformat' => array(
'choices' => array(FORMAT_HTML, FORMAT_MOODLE, FORMAT_PLAIN, FORMAT_MARKDOWN),
'type' => PARAM_INT,
'default' => FORMAT_MOODLE,
'description' => 'Text to display after submission format.',
),
'publish_stats' => array(
'default' => 0,
'type' => PARAM_BOOL,
'description' => 'Whether stats should be published.',
),
'timeopen' => array(
'type' => PARAM_INT,
'optional' => true,
'description' => 'Allow answers from this time.',
),
'timeclose' => array(
'type' => PARAM_INT,
'optional' => true,
'description' => 'Allow answers until this time.',
),
'timemodified' => array(
'type' => PARAM_INT,
'optional' => true,
'description' => 'The time this record was modified.',
),
'completionsubmit' => array(
'default' => 0,
'type' => PARAM_BOOL,
'description' => 'If this field is set to 1, then the activity will be automatically marked as complete on submission.',
),
);
}
protected static function define_related() {
return array(
'context' => 'context'
);
}
protected static function define_other_properties() {
return array(
'coursemodule' => array(
'type' => PARAM_INT
),
'introfiles' => array(
'type' => external_files::get_properties_for_exporter(),
'multiple' => true
),
'pageaftersubmitfiles' => array(
'type' => external_files::get_properties_for_exporter(),
'multiple' => true,
'optional' => true
),
);
}
protected function get_other_values(renderer_base $output) {
$context = $this->related['context'];
$values = array(
'coursemodule' => $context->instanceid,
);
$values['introfiles'] = external_util::get_area_files($context->id, 'mod_feedback', 'intro', false, false);
if (!empty($this->data->page_after_submit)) {
$values['pageaftersubmitfiles'] = external_util::get_area_files($context->id, 'mod_feedback', 'page_after_submit');
}
return $values;
}
/**
* Get the formatting parameters for the intro.
*
* @return array
*/
protected function get_format_parameters_for_intro() {
return [
'component' => 'mod_feedback',
'filearea' => 'intro',
];
}
/**
* Get the formatting parameters for the page_after_submit.
*
* @return array
*/
protected function get_format_parameters_for_page_after_submit() {
return [
'component' => 'mod_feedback',
'filearea' => 'page_after_submit',
'itemid' => 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/>.
/**
* Feedback external functions and service definitions.
*
* @package mod_feedback
* @category external
* @copyright 2017 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.3
*/
defined('MOODLE_INTERNAL') || die;
$functions = array(
'mod_feedback_get_feedbacks_by_courses' => array(
'classname' => 'mod_feedback_external',
'methodname' => 'get_feedbacks_by_courses',
'description' => 'Returns a list of feedbacks in a provided list of courses, if no list is provided all feedbacks that
the user can view will be returned.',
'type' => 'read',
'capabilities' => 'mod/feedback:view',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),
);
<?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/>.
/**
* Feedback module external functions tests
*
* @package mod_feedback
* @category external
* @copyright 2017 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.3
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
use mod_feedback\external\feedback_summary_exporter;
/**
* Feedback module external functions tests
*
* @package mod_feedback
* @category external
* @copyright 2017 Juan Leyva <juan@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.3
*/
class mod_feedback_external_testcase extends externallib_advanced_testcase {
/**
* Set up for every test
*/
public function setUp() {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
// Setup test data.
$this->course = $this->getDataGenerator()->create_course();
$this->feedback = $this->getDataGenerator()->create_module('feedback', array('course' => $this->course->id));
$this->context = context_module::instance($this->feedback->cmid);
$this->cm = get_coursemodule_from_instance('feedback', $this->feedback->id);
// Create users.
$this->student = self::getDataGenerator()->create_user();
$this->teacher = self::getDataGenerator()->create_user();
// Users enrolments.
$this->studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
$this->getDataGenerator()->enrol_user($this->student->id, $this->course->id, $this->studentrole->id, 'manual');
$this->getDataGenerator()->enrol_user($this->teacher->id, $this->course->id, $this->teacherrole->id, 'manual');
}
/**
* Test test_mod_feedback_get_feedbacks_by_courses
*/
public function test_mod_feedback_get_feedbacks_by_courses() {
global $DB;
// Create additional course.
$course2 = self::getDataGenerator()->create_course();
// Second feedback.
$record = new stdClass();
$record->course = $course2->id;
$feedback2 = self::getDataGenerator()->create_module('feedback', $record);
// Execute real Moodle enrolment as we'll call unenrol() method on the instance later.
$enrol = enrol_get_plugin('manual');
$enrolinstances = enrol_get_instances($course2->id, true);
foreach ($enrolinstances as $courseenrolinstance) {
if ($courseenrolinstance->enrol == "manual") {
$instance2 = $courseenrolinstance;
break;
}
}
$enrol->enrol_user($instance2, $this->student->id, $this->studentrole->id);
self::setUser($this->student);
$returndescription = mod_feedback_external::get_feedbacks_by_courses_returns();
// Create what we expect to be returned when querying the two courses.
// First for the student user.
$expectedfields = array('id', 'coursemodule', 'course', 'name', 'intro', 'introformat', 'introfiles', 'anonymous',
'multiple_submit', 'autonumbering', 'page_after_submitformat', 'publish_stats', 'completionsubmit');
$properties = feedback_summary_exporter::read_properties_definition();
// Add expected coursemodule and data.
$feedback1 = $this->feedback;
$feedback1->coursemodule = $feedback1->cmid;
$feedback1->introformat = 1;
$feedback1->introfiles = [];
$feedback2->coursemodule = $feedback2->cmid;
$feedback2->introformat = 1;
$feedback2->introfiles = [];
foreach ($expectedfields as $field) {
if (!empty($properties[$field]) && $properties[$field]['type'] == PARAM_BOOL) {
$feedback1->{$field} = (bool) $feedback1->{$field};
$feedback2->{$field} = (bool) $feedback2->{$field};
}
$expected1[$field] = $feedback1->{$field};
$expected2[$field] = $feedback2->{$field};
}
$expectedfeedbacks = array($expected2, $expected1);
// Call the external function passing course ids.
$result = mod_feedback_external::get_feedbacks_by_courses(array($course2->id, $this->course->id));
$result = external_api::clean_returnvalue($returndescription, $result);
$this->assertEquals($expectedfeedbacks, $result['feedbacks']);
$this->assertCount(0, $result['warnings']);
// Call the external function without passing course id.
$result = mod_feedback_external::get_feedbacks_by_courses();
$result = external_api::clean_returnvalue($returndescription, $result);
$this->assertEquals($expectedfeedbacks, $result['feedbacks']);
$this->assertCount(0, $result['warnings']);
// Unenrol user from second course and alter expected feedbacks.
$enrol->unenrol_user($instance2, $this->student->id);
array_shift($expectedfeedbacks);
// Call the external function without passing course id.
$result = mod_feedback_external::get_feedbacks_by_courses();
$result = external_api::clean_returnvalue($returndescription, $result);
$this->assertEquals($expectedfeedbacks, $result['feedbacks']);
// Call for the second course we unenrolled the user from, expected warning.
$result = mod_feedback_external::get_feedbacks_by_courses(array($course2->id));
$this->assertCount(1, $result['warnings']);
$this->assertEquals('1', $result['warnings'][0]['warningcode']);
$this->assertEquals($course2->id, $result['warnings'][0]['itemid']);
// Now, try as a teacher for getting all the additional fields.
self::setUser($this->teacher);
$additionalfields = array('email_notification', 'site_after_submit', 'page_after_submit', 'timeopen', 'timeclose',
'timemodified', 'pageaftersubmitfiles');
$feedback1->pageaftersubmitfiles = [];
foreach ($additionalfields as $field) {
if (!empty($properties[$field]) && $properties[$field]['type'] == PARAM_BOOL) {
$feedback1->{$field} = (bool) $feedback1->{$field};
}
$expectedfeedbacks[0][$field] = $feedback1->{$field};
}
$expectedfeedbacks[0]['page_after_submitformat'] = 1;
$result = mod_feedback_external::get_feedbacks_by_courses();
$result = external_api::clean_returnvalue($returndescription, $result);
$this->assertEquals($expectedfeedbacks, $result['feedbacks']);
// Admin also should get all the information.
self::setAdminUser();
$result = mod_feedback_external::get_feedbacks_by_courses(array($this->course->id));
$result = external_api::clean_returnvalue($returndescription, $result);
$this->assertEquals($expectedfeedbacks, $result['feedbacks']);
}
}
......@@ -24,7 +24,7 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2016120500; // The current module version (Date: YYYYMMDDXX)
$plugin->version = 2016120501; // The current module version (Date: YYYYMMDDXX)
$plugin->requires = 2016112900; // Requires this Moodle version
$plugin->component = 'mod_feedback'; // Full name of the plugin (used for diagnostics)
$plugin->cron = 0;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment