first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
@@ -0,0 +1,81 @@
<?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/>.
/**
* Activity base class.
*
* @package mod_choice
* @copyright 2017 onwards Ankit Agarwal <ankit.agrr@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_choice\analytics\indicator;
defined('MOODLE_INTERNAL') || die();
/**
* Activity base class.
*
* @package mod_choice
* @copyright 2017 onwards Ankit Agarwal <ankit.agrr@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class activity_base extends \core_analytics\local\indicator\community_of_inquiry_activity {
/**
* feedback_viewed_events
*
* @return string[]
*/
protected function feedback_viewed_events() {
return array('\mod_choice\event\course_module_viewed', '\mod_choice\event\answer_updated');
}
/**
* feedback_viewed
*
* @param \cm_info $cm
* @param int $contextid
* @param int $userid
* @param int $after
* @return bool
*/
protected function feedback_viewed(\cm_info $cm, $contextid, $userid, $after = null) {
// If results are shown after they answer a write action counts as feedback viewed.
if ($this->instancedata[$cm->instance]->showresults == 1) {
// The user id will be enough for any_write_log.
$user = (object)['id' => $userid];
return $this->any_write_log($contextid, $user);
}
$after = null;
if ($this->instancedata[$cm->instance]->timeclose) {
$after = $this->instancedata[$cm->instance]->timeclose;
}
return $this->feedback_post_action($cm, $contextid, $userid, $this->feedback_viewed_events(), $after);
}
/**
* Returns the name of the field that controls activity availability.
*
* @return null|string
*/
protected function get_timeclose_field() {
return 'timeclose';
}
}
@@ -0,0 +1,63 @@
<?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/>.
/**
* Cognitive depth indicator - choice.
*
* @package mod_choice
* @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_choice\analytics\indicator;
defined('MOODLE_INTERNAL') || die();
/**
* Cognitive depth indicator - choice.
*
* @package mod_choice
* @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cognitive_depth extends activity_base {
/**
* Returns the name.
*
* If there is a corresponding '_help' string this will be shown as well.
*
* @return \lang_string
*/
public static function get_name(): \lang_string {
return new \lang_string('indicator:cognitivedepth', 'mod_choice');
}
public function get_indicator_type() {
return self::INDICATOR_COGNITIVE;
}
public function get_cognitive_depth_level(\cm_info $cm) {
$this->fill_instance_data($cm);
if ($this->instancedata[$cm->instance]->showresults == 0 || $this->instancedata[$cm->instance]->showresults == 4) {
// Results are not shown to students or are always shown.
return self::COGNITIVE_LEVEL_2;
}
return self::COGNITIVE_LEVEL_3;
}
}
@@ -0,0 +1,57 @@
<?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/>.
/**
* Social breadth indicator - choice.
*
* @package mod_choice
* @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_choice\analytics\indicator;
defined('MOODLE_INTERNAL') || die();
/**
* Social breadth indicator - choice.
*
* @package mod_choice
* @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class social_breadth extends activity_base {
/**
* Returns the name.
*
* If there is a corresponding '_help' string this will be shown as well.
*
* @return \lang_string
*/
public static function get_name(): \lang_string {
return new \lang_string('indicator:socialbreadth', 'mod_choice');
}
public function get_indicator_type() {
return self::INDICATOR_SOCIAL;
}
public function get_social_breadth_level(\cm_info $cm) {
$this->fill_instance_data($cm);
return self::SOCIAL_LEVEL_2;
}
}
@@ -0,0 +1,84 @@
<?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/>.
declare(strict_types = 1);
namespace mod_choice\completion;
use core_completion\activity_custom_completion;
/**
* Activity custom completion subclass for the choice activity.
*
* Class for defining mod_choice's custom completion rules and fetching the completion statuses
* of the custom completion rules for a given choice instance and a user.
*
* @package mod_choice
* @copyright 2021 Jun Pataleta <jun@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class custom_completion extends activity_custom_completion {
/**
* Fetches the completion state for a given completion rule.
*
* @param string $rule The completion rule.
* @return int The completion state.
*/
public function get_state(string $rule): int {
global $DB;
$this->validate_rule($rule);
// Choice only supports completionsubmit as a custom rule.
$status = $DB->record_exists('choice_answers', ['choiceid' => $this->cm->instance, 'userid' => $this->userid]);
return $status ? COMPLETION_COMPLETE : COMPLETION_INCOMPLETE;
}
/**
* Fetch the list of custom completion rules that this module defines.
*
* @return array
*/
public static function get_defined_custom_rules(): array {
return [
'completionsubmit'
];
}
/**
* Returns an associative array of the descriptions of custom completion rules.
*
* @return array
*/
public function get_custom_rule_descriptions(): array {
return [
'completionsubmit' => get_string('completiondetail:submit', 'choice')
];
}
/**
* Returns an array of all completion rules, in the order they should be displayed to users.
*
* @return array
*/
public function get_sort_order(): array {
return [
'completionview',
'completionsubmit',
];
}
}
+70
View File
@@ -0,0 +1,70 @@
<?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/>.
/**
* Contains the class for fetching the important dates in mod_choice for a given module instance and a user.
*
* @package mod_choice
* @copyright Shamim Rezaie <shamim@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
declare(strict_types=1);
namespace mod_choice;
use core\activity_dates;
/**
* Class for fetching the important dates in mod_choice for a given module instance and a user.
*
* @copyright Shamim Rezaie <shamim@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class dates extends activity_dates {
/**
* Returns a list of important dates in mod_choice
*
* @return array
*/
protected function get_dates(): array {
$timeopen = $this->cm->customdata['timeopen'] ?? null;
$timeclose = $this->cm->customdata['timeclose'] ?? null;
$now = time();
$dates = [];
if ($timeopen) {
$openlabelid = $timeopen > $now ? 'activitydate:opens' : 'activitydate:opened';
$dates[] = [
'dataid' => 'timeopen',
'label' => get_string($openlabelid, 'course'),
'timestamp' => (int) $timeopen,
];
}
if ($timeclose) {
$closelabelid = $timeclose > $now ? 'activitydate:closes' : 'activitydate:closed';
$dates[] = [
'dataid' => 'timeclose',
'label' => get_string($closelabelid, 'course'),
'timestamp' => (int) $timeclose,
];
}
return $dates;
}
}
+154
View File
@@ -0,0 +1,154 @@
<?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/>.
/**
* The mod_choice answer created event.
*
* @package mod_choice
* @copyright 2016 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_choice\event;
defined('MOODLE_INTERNAL') || die();
/**
* The mod_choice answer created event class.
*
* @property-read array $other {
* Extra information about event.
*
* - int choiceid: id of choice.
* - int optionid: id of the option.
* }
*
* @package mod_choice
* @since Moodle 3.2
* @copyright 2016 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class answer_created extends \core\event\base {
/**
* Creates an instance of the event from the records
*
* @param stdClass $choiceanswer record from 'choice_answers' table
* @param stdClass $choice record from 'choice' table
* @param stdClass $cm record from 'course_modules' table
* @param stdClass $course
* @return self
*/
public static function create_from_object($choiceanswer, $choice, $cm, $course) {
global $USER;
$eventdata = array();
$eventdata['objectid'] = $choiceanswer->id;
$eventdata['context'] = \context_module::instance($cm->id);
$eventdata['userid'] = $USER->id;
$eventdata['courseid'] = $course->id;
$eventdata['relateduserid'] = $choiceanswer->userid;
$eventdata['other'] = array();
$eventdata['other']['choiceid'] = $choice->id;
$eventdata['other']['optionid'] = $choiceanswer->optionid;
$event = self::create($eventdata);
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('choice', $choice);
$event->add_record_snapshot('choice_answers', $choiceanswer);
return $event;
}
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "The user with id '$this->userid' has added the option with id '" . $this->other['optionid'] . "' for the
user with id '$this->relateduserid' from the choice activity with course module id '$this->contextinstanceid'.";
}
/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventanswercreated', 'mod_choice');
}
/**
* Get URL related to the action
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/mod/choice/view.php', array('id' => $this->contextinstanceid));
}
/**
* Init method.
*
* @return void
*/
protected function init() {
$this->data['objecttable'] = 'choice_answers';
$this->data['crud'] = 'c';
$this->data['edulevel'] = self::LEVEL_PARTICIPATING;
}
/**
* Custom validation.
*
* @throws \coding_exception
* @return void
*/
protected function validate_data() {
parent::validate_data();
if (!isset($this->other['choiceid'])) {
throw new \coding_exception('The \'choiceid\' value must be set in other.');
}
if (!isset($this->other['optionid'])) {
throw new \coding_exception('The \'optionid\' value must be set in other.');
}
}
/**
* This is used when restoring course logs where it is required that we
* map the objectid to it's new value in the new course.
*
* @return string the name of the restore mapping the objectid links to
*/
public static function get_objectid_mapping() {
return array('db' => 'choice_answers', 'restore' => 'answer');
}
/**
* This is used when restoring course logs where it is required that we
* map the information in 'other' to it's new value in the new course.
*
* @return array an array of other values and their corresponding mapping
*/
public static function get_other_mapping() {
$othermapped = array();
$othermapped['choiceid'] = array('db' => 'choice', 'restore' => 'choice');
$othermapped['optionid'] = array('db' => 'choice_options', 'restore' => 'choice_option');
return $othermapped;
}
}
+142
View File
@@ -0,0 +1,142 @@
<?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/>.
/**
* The mod_choice answer deleted event.
*
* @package mod_choice
* @copyright 2016 Stephen Bourget
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_choice\event;
defined('MOODLE_INTERNAL') || die();
/**
* The mod_choice answer deleted event class.
*
* @property-read array $other {
* Extra information about event.
*
* - int choiceid: id of choice.
* - int optionid: id of the option.
* }
*
* @package mod_choice
* @since Moodle 3.1
* @copyright 2016 Stephen Bourget
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class answer_deleted extends \core\event\base {
/**
* Creates an instance of the event from the records
*
* @param stdClass $choiceanswer record from 'choice_answers' table
* @param stdClass $choice record from 'choice' table
* @param stdClass $cm record from 'course_modules' table
* @param stdClass $course
* @return self
*/
public static function create_from_object($choiceanswer, $choice, $cm, $course) {
global $USER;
$eventdata = array();
$eventdata['objectid'] = $choiceanswer->id;
$eventdata['context'] = \context_module::instance($cm->id);
$eventdata['userid'] = $USER->id;
$eventdata['courseid'] = $course->id;
$eventdata['relateduserid'] = $choiceanswer->userid;
$eventdata['other'] = array();
$eventdata['other']['choiceid'] = $choice->id;
$eventdata['other']['optionid'] = $choiceanswer->optionid;
$event = self::create($eventdata);
$event->add_record_snapshot('course', $course);
$event->add_record_snapshot('course_modules', $cm);
$event->add_record_snapshot('choice', $choice);
$event->add_record_snapshot('choice_answers', $choiceanswer);
return $event;
}
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "The user with id '$this->userid' has deleted the option with id '" . $this->other['optionid'] . "' for the
user with id '$this->relateduserid' from the choice activity with course module id '$this->contextinstanceid'.";
}
/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventanswerdeleted', 'mod_choice');
}
/**
* Get URL related to the action
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/mod/choice/view.php', array('id' => $this->contextinstanceid));
}
/**
* Init method.
*
* @return void
*/
protected function init() {
$this->data['objecttable'] = 'choice_answers';
$this->data['crud'] = 'd';
$this->data['edulevel'] = self::LEVEL_PARTICIPATING;
}
/**
* Custom validation.
*
* @throws \coding_exception
* @return void
*/
protected function validate_data() {
parent::validate_data();
if (!isset($this->other['choiceid'])) {
throw new \coding_exception('The \'choiceid\' value must be set in other.');
}
if (!isset($this->other['optionid'])) {
throw new \coding_exception('The \'optionid\' value must be set in other.');
}
}
public static function get_objectid_mapping() {
return array('db' => 'choice_answers', 'restore' => \core\event\base::NOT_MAPPED);
}
public static function get_other_mapping() {
$othermapped = array();
$othermapped['choiceid'] = array('db' => 'choice', 'restore' => 'choice');
$othermapped['optionid'] = array('db' => 'choice_options', 'restore' => 'choice_option');
return $othermapped;
}
}
@@ -0,0 +1,136 @@
<?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/>.
/**
* The mod_choice answer submitted event.
*
* @package mod_choice
* @copyright 2013 Adrian Greeve <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_choice\event;
defined('MOODLE_INTERNAL') || die();
/**
* The mod_choice answer submitted event class.
*
* This event is deprecated in Moodle 3.2, it can no longer be triggered, do not
* write event observers for it. This event can only be initiated during
* restore from previous Moodle versions and appear in the logs.
*
* Event observers should listen to mod_choice\event\answer_created instead that
* will be triggered once for each option selected
*
* @property-read array $other {
* Extra information about event.
*
* - int choiceid: id of choice.
* - int optionid: (optional) id of option.
* }
*
* @deprecated since 3.2
* @package mod_choice
* @since Moodle 2.6
* @copyright 2013 Adrian Greeve <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class answer_submitted extends \core\event\base {
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "The user with id '$this->userid' made the choice with id '$this->objectid' in the choice activity
with course module id '$this->contextinstanceid'.";
}
/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventanswersubmitted', 'mod_choice');
}
/**
* Get URL related to the action
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/mod/choice/view.php', array('id' => $this->contextinstanceid));
}
/**
* Init method.
*
* @return void
*/
protected function init() {
// The objecttable here is wrong. We are submitting an answer, not a choice activity.
// This also makes the description misleading as it states we made a choice with id
// '$this->objectid' which just refers to the 'choice' table. The trigger for
// this event should be triggered after we insert to the 'choice_answers' table.
$this->data['crud'] = 'c';
$this->data['edulevel'] = self::LEVEL_PARTICIPATING;
$this->data['objecttable'] = 'choice';
}
/**
* Custom validation.
*
* @throws \coding_exception
* @return void
*/
protected function validate_data() {
parent::validate_data();
debugging('Event \\mod_choice\event\\answer_submitted should not be used '
. 'any more for triggering new events and can only be initiated during restore. '
. 'For new events please use \\mod_choice\\event\\answer_created', DEBUG_DEVELOPER);
if (!isset($this->other['choiceid'])) {
throw new \coding_exception('The \'choiceid\' value must be set in other.');
}
}
public static function get_objectid_mapping() {
return array('db' => 'choice', 'restore' => 'choice');
}
public static function get_other_mapping() {
$othermapped = array();
$othermapped['choiceid'] = array('db' => 'choice', 'restore' => 'choice');
// The 'optionid' is being passed as an array, so we can't map it. The event is
// triggered each time a choice is answered, where it may be possible to select
// multiple choices, so the value is converted to an array, which is then passed
// to the event. Ideally this event should be triggered every time we insert to the
// 'choice_answers' table so this will only be an int.
$othermapped['optionid'] = \core\event\base::NOT_MAPPED;
return $othermapped;
}
public static function is_deprecated() {
return true;
}
}
+138
View File
@@ -0,0 +1,138 @@
<?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/>.
/**
* The mod_choice answer updated event.
*
* @package mod_choice
* @copyright 2013 Adrian Greeve <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_choice\event;
defined('MOODLE_INTERNAL') || die();
/**
* The mod_choice answer updated event class.
*
* This event is deprecated in Moodle 3.2, it can no longer be triggered, do not
* write event observers for it. This event can only be initiated during
* restore from previous Moodle versions and appear in the logs.
*
* Event observers should listen to mod_choice\event\answer_created and
* mod_choice\event\answer_deleted instead, these events will be triggered for
* each option that was user has selected or unselected
*
* @property-read array $other {
* Extra information about event.
*
* - int choiceid: id of choice.
* - int optionid: (optional) id of option.
* }
*
* @deprecated since 3.2
* @package mod_choice
* @since Moodle 2.6
* @copyright 2013 Adrian Greeve <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class answer_updated extends \core\event\base {
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "The user with id '$this->userid' updated their choice with id '$this->objectid' in the choice activity
with course module id '$this->contextinstanceid'.";
}
/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventanswerupdated', 'mod_choice');
}
/**
* Get URL related to the action
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/mod/choice/view.php', array('id' => $this->contextinstanceid));
}
/**
* Init method.
*
* @return void
*/
protected function init() {
// The objecttable here is wrong. We are updating an answer, not a choice activity.
// This also makes the description misleading as it states we made a choice with id
// '$this->objectid' which just refers to the 'choice' table. The trigger for
// this event should be triggered after we update the 'choice_answers' table.
$this->data['crud'] = 'u';
$this->data['edulevel'] = self::LEVEL_PARTICIPATING;
$this->data['objecttable'] = 'choice';
}
/**
* Custom validation.
*
* @throws \coding_exception
* @return void
*/
protected function validate_data() {
parent::validate_data();
debugging('Event \\mod_choice\event\\answer_updated should not be used '
. 'any more for triggering new events and can only be initiated during restore. '
. 'For new events please use \\mod_choice\\event\\answer_created '
. 'and \\mod_choice\\event\\answer_deleted', DEBUG_DEVELOPER);
if (!isset($this->other['choiceid'])) {
throw new \coding_exception('The \'choiceid\' value must be set in other.');
}
}
public static function get_objectid_mapping() {
return array('db' => 'choice', 'restore' => 'choice');
}
public static function get_other_mapping() {
$othermapped = array();
$othermapped['choiceid'] = array('db' => 'choice', 'restore' => 'choice');
// The 'optionid' is being passed as an array, so we can't map it. The event is
// triggered each time a choice is answered, where it may be possible to select
// multiple choices, so the value is converted to an array, which is then passed
// to the event. Ideally this event should be triggered every time we update the
// 'choice_answers' table so this will only be an int.
$othermapped['optionid'] = \core\event\base::NOT_MAPPED;
return $othermapped;
}
public static function is_deprecated() {
return true;
}
}
@@ -0,0 +1,38 @@
<?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/>.
/**
* The mod_choice instance list viewed event.
*
* @package mod_choice
* @copyright 2013 Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_choice\event;
defined('MOODLE_INTERNAL') || die();
/**
* The mod_choice instance list viewed event class.
*
* @package mod_choice
* @since Moodle 2.7
* @copyright 2013 onwards Ankit Agarwal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class course_module_instance_list_viewed extends \core\event\course_module_instance_list_viewed {
}
@@ -0,0 +1,50 @@
<?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/>.
/**
* The mod_choice course module viewed event.
*
* @package mod_choice
* @copyright 2013 Adrian Greeve <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_choice\event;
defined('MOODLE_INTERNAL') || die();
/**
* The mod_choice course module viewed event class.
*
* @package mod_choice
* @since Moodle 2.6
* @copyright 2013 Adrian Greeve
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class course_module_viewed extends \core\event\course_module_viewed {
/**
* Init method.
*/
protected function init() {
$this->data['crud'] = 'r';
$this->data['edulevel'] = self::LEVEL_PARTICIPATING;
$this->data['objecttable'] = 'choice';
}
public static function get_objectid_mapping() {
return array('db' => 'choice', 'restore' => 'choice');
}
}
@@ -0,0 +1,114 @@
<?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/>.
/**
* The mod_choice report viewed event.
*
* @package mod_choice
* @copyright 2016 Stephen Bourget
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_choice\event;
defined('MOODLE_INTERNAL') || die();
/**
* The mod_choice report viewed event class.
*
* @property-read array $other {
* Extra information about the event.
*
* - string content: The content we are viewing.
* - string format: The report format
* - int choiced: The id of the choice
* }
*
* @package mod_choice
* @since Moodle 3.1
* @copyright 2016 Stephen Bourget
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class report_downloaded extends \core\event\base {
/**
* Init method.
*/
protected function init() {
$this->data['crud'] = 'r';
$this->data['edulevel'] = self::LEVEL_TEACHING;
}
/**
* Returns localised general event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventreportdownloaded', 'mod_choice');
}
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "The user with id '$this->userid' has downloaded the report in the '".$this->other['format']."' format for
the choice activity with course module id '$this->contextinstanceid'";
}
/**
* Returns relevant URL.
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/mod/choice/report.php', array('id' => $this->contextinstanceid));
}
/**
* Custom validation.
*
* @throws \coding_exception
* @return void
*/
protected function validate_data() {
parent::validate_data();
// Report format downloaded.
if (!isset($this->other['content'])) {
throw new \coding_exception('The \'content\' value must be set in other.');
}
// Report format downloaded.
if (!isset($this->other['format'])) {
throw new \coding_exception('The \'format\' value must be set in other.');
}
// ID of the choice activity.
if (!isset($this->other['choiceid'])) {
throw new \coding_exception('The \'choiceid\' value must be set in other.');
}
}
public static function get_objectid_mapping() {
return false;
}
public static function get_other_mapping() {
$othermapped = array();
$othermapped['choiceid'] = array('db' => 'choice', 'restore' => 'choice');
return $othermapped;
}
}
@@ -0,0 +1,88 @@
<?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/>.
/**
* The mod_choice report viewed event.
*
* @package mod_choice
* @copyright 2013 Adrian Greeve <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_choice\event;
defined('MOODLE_INTERNAL') || die();
/**
* The mod_choice report viewed event class.
*
* @property-read array $other {
* Extra information about the event.
*
* - string content: (optional) The content we are viewing.
* }
*
* @package mod_choice
* @since Moodle 2.6
* @copyright 2013 Adrian Greeve
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class report_viewed extends \core\event\base {
/**
* Init method.
*/
protected function init() {
$this->data['crud'] = 'r';
$this->data['edulevel'] = self::LEVEL_TEACHING;
$this->data['objecttable'] = 'choice';
}
/**
* Returns localised general event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventreportviewed', 'mod_choice');
}
/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "The user with id '$this->userid' has viewed the report for the choice activity with course module id
'$this->contextinstanceid'";
}
/**
* Returns relevant URL.
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/mod/choice/report.php', array('id' => $this->contextinstanceid));
}
public static function get_objectid_mapping() {
return array('db' => 'choice', 'restore' => 'choice');
}
public static function get_other_mapping() {
// No need to map the 'content' value.
return false;
}
}
+685
View File
@@ -0,0 +1,685 @@
<?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/>.
/**
* Choice module external API
*
* @package mod_choice
* @category external
* @copyright 2015 Costantino Cito <ccito@cvaconsulting.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.0
*/
use core_course\external\helper_for_get_mods_by_courses;
use core_external\external_api;
use core_external\external_function_parameters;
use core_external\external_multiple_structure;
use core_external\external_single_structure;
use core_external\external_value;
use core_external\external_warnings;
use core_external\util;
defined('MOODLE_INTERNAL') || die;
require_once($CFG->dirroot . '/mod/choice/lib.php');
/**
* Choice module external functions
*
* @package mod_choice
* @category external
* @copyright 2015 Costantino Cito <ccito@cvaconsulting.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.0
*/
class mod_choice_external extends external_api {
/**
* Describes the parameters for get_choices_by_courses.
*
* @return external_function_parameters
* @since Moodle 3.0
*/
public static function get_choice_results_parameters() {
return new external_function_parameters (array('choiceid' => new external_value(PARAM_INT, 'choice instance id')));
}
/**
* Returns user's results for a specific choice
* and a list of those users that did not answered yet.
*
* @param int $choiceid the choice instance id
* @return array of responses details
* @since Moodle 3.0
*/
public static function get_choice_results($choiceid) {
global $USER, $PAGE;
$params = self::validate_parameters(self::get_choice_results_parameters(), array('choiceid' => $choiceid));
if (!$choice = choice_get_choice($params['choiceid'])) {
throw new moodle_exception("invalidcoursemodule", "error");
}
list($course, $cm) = get_course_and_cm_from_instance($choice, 'choice');
$context = context_module::instance($cm->id);
self::validate_context($context);
$groupmode = groups_get_activity_groupmode($cm);
// Check if we have to include responses from inactive users.
$onlyactive = $choice->includeinactive ? false : true;
$users = choice_get_response_data($choice, $cm, $groupmode, $onlyactive);
// Show those who haven't answered the question.
if (!empty($choice->showunanswered)) {
$choice->option[0] = get_string('notanswered', 'choice');
$choice->maxanswers[0] = 0;
}
$results = prepare_choice_show_results($choice, $course, $cm, $users);
$options = array();
$fullnamecap = has_capability('moodle/site:viewfullnames', $context);
foreach ($results->options as $optionid => $option) {
$userresponses = array();
$numberofuser = 0;
$percentageamount = 0;
if (property_exists($option, 'user') and
(has_capability('mod/choice:readresponses', $context) or choice_can_view_results($choice))) {
$numberofuser = count($option->user);
$percentageamount = ((float)$numberofuser / (float)$results->numberofuser) * 100.0;
if ($choice->publish) {
foreach ($option->user as $userresponse) {
$response = array();
$response['userid'] = $userresponse->id;
$response['fullname'] = fullname($userresponse, $fullnamecap);
$userpicture = new user_picture($userresponse);
$userpicture->size = 1; // Size f1.
$response['profileimageurl'] = $userpicture->get_url($PAGE)->out(false);
// Add optional properties.
foreach (array('answerid', 'timemodified') as $field) {
if (property_exists($userresponse, 'answerid')) {
$response[$field] = $userresponse->$field;
}
}
$userresponses[] = $response;
}
}
}
$options[] = array('id' => $optionid,
'text' => \core_external\util::format_string($option->text, $context->id),
'maxanswer' => $option->maxanswer,
'userresponses' => $userresponses,
'numberofuser' => $numberofuser,
'percentageamount' => $percentageamount
);
}
$warnings = array();
return array(
'options' => $options,
'warnings' => $warnings
);
}
/**
* Describes the get_choice_results return value.
*
* @return external_single_structure
* @since Moodle 3.0
*/
public static function get_choice_results_returns() {
return new external_single_structure(
array(
'options' => new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'choice instance id'),
'text' => new external_value(PARAM_RAW, 'text of the choice'),
'maxanswer' => new external_value(PARAM_INT, 'maximum number of answers'),
'userresponses' => new external_multiple_structure(
new external_single_structure(
array(
'userid' => new external_value(PARAM_INT, 'user id'),
'fullname' => new external_value(PARAM_NOTAGS, 'user full name'),
'profileimageurl' => new external_value(PARAM_URL, 'profile user image url'),
'answerid' => new external_value(PARAM_INT, 'answer id', VALUE_OPTIONAL),
'timemodified' => new external_value(PARAM_INT, 'time of modification', VALUE_OPTIONAL),
), 'User responses'
)
),
'numberofuser' => new external_value(PARAM_INT, 'number of users answers'),
'percentageamount' => new external_value(PARAM_FLOAT, 'percentage of users answers')
), 'Options'
)
),
'warnings' => new external_warnings(),
)
);
}
/**
* Describes the parameters for mod_choice_get_choice_options.
*
* @return external_function_parameters
* @since Moodle 3.0
*/
public static function get_choice_options_parameters() {
return new external_function_parameters (array('choiceid' => new external_value(PARAM_INT, 'choice instance id')));
}
/**
* Returns options for a specific choice
*
* @param int $choiceid the choice instance id
* @return array of options details
* @since Moodle 3.0
*/
public static function get_choice_options($choiceid) {
global $USER;
$warnings = array();
$params = self::validate_parameters(self::get_choice_options_parameters(), array('choiceid' => $choiceid));
if (!$choice = choice_get_choice($params['choiceid'])) {
throw new moodle_exception("invalidcoursemodule", "error");
}
list($course, $cm) = get_course_and_cm_from_instance($choice, 'choice');
$context = context_module::instance($cm->id);
self::validate_context($context);
require_capability('mod/choice:choose', $context);
$groupmode = groups_get_activity_groupmode($cm);
$onlyactive = $choice->includeinactive ? false : true;
$allresponses = choice_get_response_data($choice, $cm, $groupmode, $onlyactive);
$timenow = time();
$choiceopen = true;
$showpreview = false;
if (!empty($choice->timeopen) && ($choice->timeopen > $timenow)) {
$choiceopen = false;
$warnings[1] = get_string("notopenyet", "choice", userdate($choice->timeopen));
if ($choice->showpreview) {
$warnings[2] = get_string('previewonly', 'choice', userdate($choice->timeopen));
$showpreview = true;
}
}
if (!empty($choice->timeclose) && ($timenow > $choice->timeclose)) {
$choiceopen = false;
$warnings[3] = get_string("expired", "choice", userdate($choice->timeclose));
}
$optionsarray = array();
if ($choiceopen or $showpreview) {
$options = choice_prepare_options($choice, $USER, $cm, $allresponses);
foreach ($options['options'] as $option) {
$optionarr = array();
$optionarr['id'] = $option->attributes->value;
$optionarr['text'] = \core_external\util::format_string($option->text, $context->id);
$optionarr['maxanswers'] = $option->maxanswers;
$optionarr['displaylayout'] = $option->displaylayout;
$optionarr['countanswers'] = $option->countanswers;
foreach (array('checked', 'disabled') as $field) {
if (property_exists($option->attributes, $field) and $option->attributes->$field == 1) {
$optionarr[$field] = 1;
} else {
$optionarr[$field] = 0;
}
}
// When showpreview is active, we show options as disabled.
if ($showpreview or ($optionarr['checked'] == 1 and !$choice->allowupdate)) {
$optionarr['disabled'] = 1;
}
$optionsarray[] = $optionarr;
}
}
foreach ($warnings as $key => $message) {
$warnings[$key] = array(
'item' => 'choice',
'itemid' => $cm->id,
'warningcode' => $key,
'message' => $message
);
}
return array(
'options' => $optionsarray,
'warnings' => $warnings
);
}
/**
* Describes the get_choice_results return value.
*
* @return external_multiple_structure
* @since Moodle 3.0
*/
public static function get_choice_options_returns() {
return new external_single_structure(
array(
'options' => new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'option id'),
'text' => new external_value(PARAM_RAW, 'text of the choice'),
'maxanswers' => new external_value(PARAM_INT, 'maximum number of answers'),
'displaylayout' => new external_value(PARAM_BOOL, 'true for orizontal, otherwise vertical'),
'countanswers' => new external_value(PARAM_INT, 'number of answers'),
'checked' => new external_value(PARAM_BOOL, 'we already answered'),
'disabled' => new external_value(PARAM_BOOL, 'option disabled'),
)
), 'Options'
),
'warnings' => new external_warnings(),
)
);
}
/**
* Describes the parameters for submit_choice_response.
*
* @return external_function_parameters
* @since Moodle 3.0
*/
public static function submit_choice_response_parameters() {
return new external_function_parameters (
array(
'choiceid' => new external_value(PARAM_INT, 'choice instance id'),
'responses' => new external_multiple_structure(
new external_value(PARAM_INT, 'answer id'),
'Array of response ids'
),
)
);
}
/**
* Submit choice responses
*
* @param int $choiceid the choice instance id
* @param array $responses the response ids
* @return array answers information and warnings
* @since Moodle 3.0
*/
public static function submit_choice_response($choiceid, $responses) {
global $USER;
$warnings = array();
$params = self::validate_parameters(self::submit_choice_response_parameters(),
array(
'choiceid' => $choiceid,
'responses' => $responses
));
if (!$choice = choice_get_choice($params['choiceid'])) {
throw new moodle_exception("invalidcoursemodule", "error");
}
list($course, $cm) = get_course_and_cm_from_instance($choice, 'choice');
$context = context_module::instance($cm->id);
self::validate_context($context);
require_capability('mod/choice:choose', $context);
$timenow = time();
if (!empty($choice->timeopen) && ($choice->timeopen > $timenow)) {
throw new moodle_exception("notopenyet", "choice", '', userdate($choice->timeopen));
} else if (!empty($choice->timeclose) && ($timenow > $choice->timeclose)) {
throw new moodle_exception("expired", "choice", '', userdate($choice->timeclose));
}
if (!choice_get_my_response($choice) or $choice->allowupdate) {
// When a single response is given, we convert the array to a simple variable
// in order to avoid choice_user_submit_response to check with allowmultiple even
// for a single response.
if (count($params['responses']) == 1) {
$params['responses'] = reset($params['responses']);
}
choice_user_submit_response($params['responses'], $choice, $USER->id, $course, $cm);
} else {
throw new moodle_exception('missingrequiredcapability', 'webservice', '', 'allowupdate');
}
$answers = choice_get_my_response($choice);
return array(
'answers' => $answers,
'warnings' => $warnings
);
}
/**
* Describes the submit_choice_response return value.
*
* @return external_multiple_structure
* @since Moodle 3.0
*/
public static function submit_choice_response_returns() {
return new external_single_structure(
array(
'answers' => new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'answer id'),
'choiceid' => new external_value(PARAM_INT, 'choiceid'),
'userid' => new external_value(PARAM_INT, 'user id'),
'optionid' => new external_value(PARAM_INT, 'optionid'),
'timemodified' => new external_value(PARAM_INT, 'time of last modification')
), 'Answers'
)
),
'warnings' => new external_warnings(),
)
);
}
/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 3.0
*/
public static function view_choice_parameters() {
return new external_function_parameters(
array(
'choiceid' => new external_value(PARAM_INT, 'choice instance id')
)
);
}
/**
* Trigger the course module viewed event and update the module completion status.
*
* @param int $choiceid the choice instance id
* @return array of warnings and status result
* @since Moodle 3.0
* @throws moodle_exception
*/
public static function view_choice($choiceid) {
global $CFG;
$params = self::validate_parameters(self::view_choice_parameters(),
array(
'choiceid' => $choiceid
));
$warnings = array();
// Request and permission validation.
if (!$choice = choice_get_choice($params['choiceid'])) {
throw new moodle_exception("invalidcoursemodule", "error");
}
list($course, $cm) = get_course_and_cm_from_instance($choice, 'choice');
$context = context_module::instance($cm->id);
self::validate_context($context);
// Trigger course_module_viewed event and completion.
choice_view($choice, $course, $cm, $context);
$result = array();
$result['status'] = true;
$result['warnings'] = $warnings;
return $result;
}
/**
* Returns description of method result value
*
* @return \core_external\external_description
* @since Moodle 3.0
*/
public static function view_choice_returns() {
return new external_single_structure(
array(
'status' => new external_value(PARAM_BOOL, 'status: true if success'),
'warnings' => new external_warnings()
)
);
}
/**
* Describes the parameters for get_choices_by_courses.
*
* @return external_function_parameters
* @since Moodle 3.0
*/
public static function get_choices_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 choices in a provided list of courses,
* if no list is provided all choices that the user can view will be returned.
*
* @param array $courseids the course ids
* @return array of choices details
* @since Moodle 3.0
*/
public static function get_choices_by_courses($courseids = array()) {
$returnedchoices = array();
$warnings = array();
$params = self::validate_parameters(self::get_choices_by_courses_parameters(), array('courseids' => $courseids));
$courses = array();
if (empty($params['courseids'])) {
$courses = enrol_get_my_courses();
$params['courseids'] = array_keys($courses);
}
// Ensure there are courseids to loop through.
if (!empty($params['courseids'])) {
list($courses, $warnings) = util::validate_courses($params['courseids'], $courses);
// Get the choices in this course, this function checks users visibility permissions.
// We can avoid then additional validate_context calls.
$choices = get_all_instances_in_courses("choice", $courses);
foreach ($choices as $choice) {
$context = context_module::instance($choice->coursemodule);
$choicedetails = helper_for_get_mods_by_courses::standard_coursemodule_element_values($choice, 'mod_choice');
if (has_capability('mod/choice:choose', $context)) {
$choicedetails['publish'] = $choice->publish;
$choicedetails['showresults'] = $choice->showresults;
$choicedetails['showpreview'] = $choice->showpreview;
$choicedetails['timeopen'] = $choice->timeopen;
$choicedetails['timeclose'] = $choice->timeclose;
$choicedetails['display'] = $choice->display;
$choicedetails['allowupdate'] = $choice->allowupdate;
$choicedetails['allowmultiple'] = $choice->allowmultiple;
$choicedetails['limitanswers'] = $choice->limitanswers;
$choicedetails['showunanswered'] = $choice->showunanswered;
$choicedetails['includeinactive'] = $choice->includeinactive;
$choicedetails['showavailable'] = $choice->showavailable;
}
if (has_capability('moodle/course:manageactivities', $context)) {
$choicedetails['timemodified'] = $choice->timemodified;
$choicedetails['completionsubmit'] = $choice->completionsubmit;
}
$returnedchoices[] = $choicedetails;
}
}
$result = array();
$result['choices'] = $returnedchoices;
$result['warnings'] = $warnings;
return $result;
}
/**
* Describes the mod_choice_get_choices_by_courses return value.
*
* @return external_single_structure
* @since Moodle 3.0
*/
public static function get_choices_by_courses_returns() {
return new external_single_structure(
array(
'choices' => new external_multiple_structure(
new external_single_structure(array_merge(
helper_for_get_mods_by_courses::standard_coursemodule_elements_returns(),
[
'publish' => new external_value(PARAM_BOOL, 'If choice is published', VALUE_OPTIONAL),
'showresults' => new external_value(PARAM_INT, '0 never, 1 after answer, 2 after close, 3 always',
VALUE_OPTIONAL),
'display' => new external_value(PARAM_INT, 'Display mode (vertical, horizontal)', VALUE_OPTIONAL),
'allowupdate' => new external_value(PARAM_BOOL, 'Allow update', VALUE_OPTIONAL),
'allowmultiple' => new external_value(PARAM_BOOL, 'Allow multiple choices', VALUE_OPTIONAL),
'showunanswered' => new external_value(PARAM_BOOL, 'Show users who not answered yet', VALUE_OPTIONAL),
'includeinactive' => new external_value(PARAM_BOOL, 'Include inactive users', VALUE_OPTIONAL),
'limitanswers' => new external_value(PARAM_BOOL, 'Limit unswers', VALUE_OPTIONAL),
'timeopen' => new external_value(PARAM_INT, 'Date of opening validity', VALUE_OPTIONAL),
'timeclose' => new external_value(PARAM_INT, 'Date of closing validity', VALUE_OPTIONAL),
'showpreview' => new external_value(PARAM_BOOL, 'Show preview before timeopen', VALUE_OPTIONAL),
'timemodified' => new external_value(PARAM_INT, 'Time of last modification', VALUE_OPTIONAL),
'completionsubmit' => new external_value(PARAM_BOOL, 'Completion on user submission', VALUE_OPTIONAL),
'showavailable' => new external_value(PARAM_BOOL, 'Show available spaces', VALUE_OPTIONAL),
]
), 'Choices')
),
'warnings' => new external_warnings(),
)
);
}
/**
* Describes the parameters for delete_choice_responses.
*
* @return external_function_parameters
* @since Moodle 3.0
*/
public static function delete_choice_responses_parameters() {
return new external_function_parameters (
array(
'choiceid' => new external_value(PARAM_INT, 'choice instance id'),
'responses' => new external_multiple_structure(
new external_value(PARAM_INT, 'response id'),
'Array of response ids, empty for deleting all the current user responses.',
VALUE_DEFAULT,
array()
),
)
);
}
/**
* Delete the given submitted responses in a choice
*
* @param int $choiceid the choice instance id
* @param array $responses the response ids, empty for deleting all the current user responses
* @return array status information and warnings
* @throws moodle_exception
* @since Moodle 3.0
*/
public static function delete_choice_responses($choiceid, $responses = array()) {
$status = false;
$warnings = array();
$params = self::validate_parameters(self::delete_choice_responses_parameters(),
array(
'choiceid' => $choiceid,
'responses' => $responses
));
if (!$choice = choice_get_choice($params['choiceid'])) {
throw new moodle_exception("invalidcoursemodule", "error");
}
list($course, $cm) = get_course_and_cm_from_instance($choice, 'choice');
$context = context_module::instance($cm->id);
self::validate_context($context);
require_capability('mod/choice:choose', $context);
$candeleteall = has_capability('mod/choice:deleteresponses', $context);
if ($candeleteall || $choice->allowupdate) {
// Check if we can delete our own responses.
if (!$candeleteall) {
$timenow = time();
if (!empty($choice->timeclose) && ($timenow > $choice->timeclose)) {
throw new moodle_exception("expired", "choice", '', userdate($choice->timeclose));
}
}
if (empty($params['responses'])) {
// No responses indicated so delete only my responses.
$todelete = array_keys(choice_get_my_response($choice));
} else {
// Fill an array with the responses that can be deleted for this choice.
if ($candeleteall) {
// Teacher/managers can delete any.
$allowedresponses = array_keys(choice_get_all_responses($choice));
} else {
// Students can delete only their own responses.
$allowedresponses = array_keys(choice_get_my_response($choice));
}
$todelete = array();
foreach ($params['responses'] as $response) {
if (!in_array($response, $allowedresponses)) {
$warnings[] = array(
'item' => 'response',
'itemid' => $response,
'warningcode' => 'nopermissions',
'message' => 'Invalid response id, the response does not exist or you are not allowed to delete it.'
);
} else {
$todelete[] = $response;
}
}
}
$status = choice_delete_responses($todelete, $choice, $cm, $course);
} else {
// The user requires the capability to delete responses.
throw new required_capability_exception($context, 'mod/choice:deleteresponses', 'nopermissions', '');
}
return array(
'status' => $status,
'warnings' => $warnings
);
}
/**
* Describes the delete_choice_responses return value.
*
* @return external_multiple_structure
* @since Moodle 3.0
*/
public static function delete_choice_responses_returns() {
return new external_single_structure(
array(
'status' => new external_value(PARAM_BOOL, 'status, true if everything went right'),
'warnings' => new external_warnings(),
)
);
}
}
+283
View File
@@ -0,0 +1,283 @@
<?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/>.
/**
* Privacy Subsystem implementation for mod_choice.
*
* @package mod_choice
* @category privacy
* @copyright 2018 Jun Pataleta
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_choice\privacy;
use core_privacy\local\metadata\collection;
use core_privacy\local\request\approved_contextlist;
use core_privacy\local\request\approved_userlist;
use core_privacy\local\request\contextlist;
use core_privacy\local\request\deletion_criteria;
use core_privacy\local\request\helper;
use core_privacy\local\request\userlist;
use core_privacy\local\request\writer;
defined('MOODLE_INTERNAL') || die();
/**
* Implementation of the privacy subsystem plugin provider for the choice activity module.
*
* @copyright 2018 Jun Pataleta
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements
// This plugin stores personal data.
\core_privacy\local\metadata\provider,
// This plugin is a core_user_data_provider.
\core_privacy\local\request\plugin\provider,
// This plugin is capable of determining which users have data within it.
\core_privacy\local\request\core_userlist_provider {
/**
* Return the fields which contain personal data.
*
* @param collection $items a reference to the collection to use to store the metadata.
* @return collection the updated collection of metadata items.
*/
public static function get_metadata(collection $items): collection {
$items->add_database_table(
'choice_answers',
[
'choiceid' => 'privacy:metadata:choice_answers:choiceid',
'optionid' => 'privacy:metadata:choice_answers:optionid',
'userid' => 'privacy:metadata:choice_answers:userid',
'timemodified' => 'privacy:metadata:choice_answers:timemodified',
],
'privacy:metadata:choice_answers'
);
return $items;
}
/**
* Get the list of contexts that contain user information for the specified user.
*
* @param int $userid the userid.
* @return contextlist the list of contexts containing user info for the user.
*/
public static function get_contexts_for_userid(int $userid): contextlist {
// Fetch all choice answers.
$sql = "SELECT c.id
FROM {context} c
INNER JOIN {course_modules} cm ON cm.id = c.instanceid AND c.contextlevel = :contextlevel
INNER JOIN {modules} m ON m.id = cm.module AND m.name = :modname
INNER JOIN {choice} ch ON ch.id = cm.instance
INNER JOIN {choice_options} co ON co.choiceid = ch.id
INNER JOIN {choice_answers} ca ON ca.optionid = co.id AND ca.choiceid = ch.id
WHERE ca.userid = :userid";
$params = [
'modname' => 'choice',
'contextlevel' => CONTEXT_MODULE,
'userid' => $userid,
];
$contextlist = new contextlist();
$contextlist->add_from_sql($sql, $params);
return $contextlist;
}
/**
* Get the list of users who have data within a context.
*
* @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
*/
public static function get_users_in_context(userlist $userlist) {
$context = $userlist->get_context();
if (!$context instanceof \context_module) {
return;
}
// Fetch all choice answers.
$sql = "SELECT ca.userid
FROM {course_modules} cm
JOIN {modules} m ON m.id = cm.module AND m.name = :modname
JOIN {choice} ch ON ch.id = cm.instance
JOIN {choice_options} co ON co.choiceid = ch.id
JOIN {choice_answers} ca ON ca.optionid = co.id AND ca.choiceid = ch.id
WHERE cm.id = :cmid";
$params = [
'cmid' => $context->instanceid,
'modname' => 'choice',
];
$userlist->add_from_sql('userid', $sql, $params);
}
/**
* Export personal data for the given approved_contextlist. User and context information is contained within the contextlist.
*
* @param approved_contextlist $contextlist a list of contexts approved for export.
*/
public static function export_user_data(approved_contextlist $contextlist) {
global $DB;
if (empty($contextlist->count())) {
return;
}
$user = $contextlist->get_user();
list($contextsql, $contextparams) = $DB->get_in_or_equal($contextlist->get_contextids(), SQL_PARAMS_NAMED);
$sql = "SELECT cm.id AS cmid,
co.text as answer,
ca.timemodified
FROM {context} c
INNER JOIN {course_modules} cm ON cm.id = c.instanceid AND c.contextlevel = :contextlevel
INNER JOIN {modules} m ON m.id = cm.module AND m.name = :modname
INNER JOIN {choice} ch ON ch.id = cm.instance
INNER JOIN {choice_options} co ON co.choiceid = ch.id
INNER JOIN {choice_answers} ca ON ca.optionid = co.id AND ca.choiceid = ch.id
WHERE c.id {$contextsql}
AND ca.userid = :userid
ORDER BY cm.id";
$params = ['modname' => 'choice', 'contextlevel' => CONTEXT_MODULE, 'userid' => $user->id] + $contextparams;
// Reference to the choice activity seen in the last iteration of the loop. By comparing this with the current record, and
// because we know the results are ordered, we know when we've moved to the answers for a new choice activity and therefore
// when we can export the complete data for the last activity.
$lastcmid = null;
$choiceanswers = $DB->get_recordset_sql($sql, $params);
foreach ($choiceanswers as $choiceanswer) {
// If we've moved to a new choice, then write the last choice data and reinit the choice data array.
if ($lastcmid != $choiceanswer->cmid) {
if (!empty($choicedata)) {
$context = \context_module::instance($lastcmid);
self::export_choice_data_for_user($choicedata, $context, $user);
}
$choicedata = [
'answer' => [],
'timemodified' => \core_privacy\local\request\transform::datetime($choiceanswer->timemodified),
];
}
$choicedata['answer'][] = $choiceanswer->answer;
$lastcmid = $choiceanswer->cmid;
}
$choiceanswers->close();
// The data for the last activity won't have been written yet, so make sure to write it now!
if (!empty($choicedata)) {
$context = \context_module::instance($lastcmid);
self::export_choice_data_for_user($choicedata, $context, $user);
}
}
/**
* Export the supplied personal data for a single choice activity, along with any generic data or area files.
*
* @param array $choicedata the personal data to export for the choice.
* @param \context_module $context the context of the choice.
* @param \stdClass $user the user record
*/
protected static function export_choice_data_for_user(array $choicedata, \context_module $context, \stdClass $user) {
// Fetch the generic module data for the choice.
$contextdata = helper::get_context_data($context, $user);
// Merge with choice data and write it.
$contextdata = (object)array_merge((array)$contextdata, $choicedata);
writer::with_context($context)->export_data([], $contextdata);
// Write generic module intro files.
helper::export_context_files($context, $user);
}
/**
* Delete all data for all users in the specified context.
*
* @param \context $context the context to delete in.
*/
public static function delete_data_for_all_users_in_context(\context $context) {
global $DB;
if (!$context instanceof \context_module) {
return;
}
if ($cm = get_coursemodule_from_id('choice', $context->instanceid)) {
$DB->delete_records('choice_answers', ['choiceid' => $cm->instance]);
}
}
/**
* Delete all user data for the specified user, in the specified contexts.
*
* @param approved_contextlist $contextlist a list of contexts approved for deletion.
*/
public static function delete_data_for_user(approved_contextlist $contextlist) {
global $DB;
if (empty($contextlist->count())) {
return;
}
$userid = $contextlist->get_user()->id;
foreach ($contextlist->get_contexts() as $context) {
if (!$context instanceof \context_module) {
continue;
}
$instanceid = $DB->get_field('course_modules', 'instance', ['id' => $context->instanceid]);
if (!$instanceid) {
continue;
}
$DB->delete_records('choice_answers', ['choiceid' => $instanceid, 'userid' => $userid]);
}
}
/**
* Delete multiple users within a single context.
*
* @param approved_userlist $userlist The approved context and user information to delete information for.
*/
public static function delete_data_for_users(approved_userlist $userlist) {
global $DB;
$context = $userlist->get_context();
if (!$context instanceof \context_module) {
return;
}
$cm = get_coursemodule_from_id('choice', $context->instanceid);
if (!$cm) {
// Only choice module will be handled.
return;
}
$userids = $userlist->get_userids();
list($usersql, $userparams) = $DB->get_in_or_equal($userids, SQL_PARAMS_NAMED);
$select = "choiceid = :choiceid AND userid $usersql";
$params = ['choiceid' => $cm->instance] + $userparams;
$DB->delete_records_select('choice_answers', $select, $params);
}
}
+46
View File
@@ -0,0 +1,46 @@
<?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/>.
/**
* Search area for mod_choice activities.
*
* @package mod_choice
* @copyright 2015 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_choice\search;
defined('MOODLE_INTERNAL') || die();
/**
* Search area for mod_choice activities.
*
* @package mod_choice
* @copyright 2015 David Monllao {@link http://www.davidmonllao.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class activity extends \core_search\base_activity {
/**
* Returns true if this area uses file indexing.
*
* @return bool
*/
public function uses_file_indexing() {
return true;
}
}