Merge branch 'wip-MDL-58665-master' of git://github.com/marinaglancy/moodle

This commit is contained in:
Andrew Nicols 2017-05-02 09:00:07 +08:00
commit db2c66e700
21 changed files with 157 additions and 106 deletions

View File

@ -729,9 +729,7 @@ function mod_book_core_calendar_provide_event_action(calendar_event $event,
return null;
}
$course = new stdClass();
$course->id = $event->courseid;
$completion = new \completion_info($course);
$completion = new \completion_info($cm->get_course());
$completiondata = $completion->get_data($cm, false);

View File

@ -1194,22 +1194,20 @@ function choice_check_updates_since(cm_info $cm, $from, $filter = array()) {
*/
function mod_choice_core_calendar_provide_event_action(calendar_event $event,
\core_calendar\action_factory $factory) {
global $DB;
$cm = get_fast_modinfo($event->courseid)->instances['choice'][$event->instance];
$choice = $DB->get_record('choice', array('id' => $event->instance), 'id, timeopen, timeclose');
$now = time();
if ($choice->timeclose && $choice->timeclose < $now) {
if (!empty($cm->customdata['timeclose']) && $cm->customdata['timeclose'] < $now) {
// The choice has closed so the user can no longer submit anything.
return null;
}
// The choice is actionable if we don't have a start time or the start time is
// in the past.
$actionable = (!$choice->timeopen || $choice->timeopen <= $now);
$actionable = (empty($cm->customdata['timeopen']) || $cm->customdata['timeopen'] <= $now);
if ($actionable && choice_get_my_response($choice)) {
if ($actionable && choice_get_my_response((object)['id' => $event->instance])) {
// There is no action if the user has already submitted their choice.
return null;
}
@ -1247,7 +1245,7 @@ function choice_get_coursemodule_info($coursemodule) {
global $DB;
$dbparams = ['id' => $coursemodule->instance];
$fields = 'id, name, intro, introformat, completionsubmit';
$fields = 'id, name, intro, introformat, completionsubmit, timeopen, timeclose';
if (!$choice = $DB->get_record('choice', $dbparams, $fields)) {
return false;
}
@ -1264,6 +1262,13 @@ function choice_get_coursemodule_info($coursemodule) {
if ($coursemodule->completion == COMPLETION_TRACKING_AUTOMATIC) {
$result->customdata['customcompletionrules']['completionsubmit'] = $choice->completionsubmit;
}
// Populate some other values that can be used in calendar or on dashboard.
if ($choice->timeopen) {
$result->customdata['timeopen'] = $choice->timeopen;
}
if ($choice->timeclose) {
$result->customdata['timeclose'] = $choice->timeclose;
}
return $result;
}

View File

@ -4298,21 +4298,19 @@ function data_check_updates_since(cm_info $cm, $from, $filter = array()) {
*/
function mod_data_core_calendar_provide_event_action(calendar_event $event,
\core_calendar\action_factory $factory) {
global $DB;
$cm = get_fast_modinfo($event->courseid)->instances['data'][$event->instance];
$data = $DB->get_record('data', array('id' => $event->instance), 'id, timeavailablefrom, timeavailableto');
$now = time();
if ($data->timeavailablefrom && $data->timeavailableto) {
$actionable = (time() >= $data->timeavailablefrom) && (time() <= $data->timeavailableto);
} else if ($data->timeavailableto) {
$actionable = time() < $data->timeavailableto;
} else if ($data->timeavailablefrom) {
$actionable = time() >= $data->timeavailablefrom;
} else {
$actionable = true;
if (!empty($cm->customdata['timeavailableto']) && $cm->customdata['timeavailableto'] < $now) {
// The module has closed so the user can no longer submit anything.
return null;
}
// The module is actionable if we don't have a start time or the start time is
// in the past.
$actionable = (empty($cm->customdata['timeavailablefrom']) || $cm->customdata['timeavailablefrom'] <= $now);
return $factory->create_instance(
get_string('add', 'data'),
new \moodle_url('/mod/data/view.php', array('id' => $cm->id)),
@ -4336,7 +4334,7 @@ function data_get_coursemodule_info($coursemodule) {
global $DB;
$dbparams = ['id' => $coursemodule->instance];
$fields = 'id, name, intro, introformat, completionentries';
$fields = 'id, name, intro, introformat, completionentries, timeavailablefrom, timeavailableto';
if (!$data = $DB->get_record('data', $dbparams, $fields)) {
return false;
}
@ -4353,6 +4351,13 @@ function data_get_coursemodule_info($coursemodule) {
if ($coursemodule->completion == COMPLETION_TRACKING_AUTOMATIC) {
$result->customdata['customcompletionrules']['completionentries'] = $data->completionentries;
}
// Other properties that may be used in calendar or on dashboard.
if ($data->timeavailablefrom) {
$result->customdata['timeavailablefrom'] = $data->timeavailablefrom;
}
if ($data->timeavailableto) {
$result->customdata['timeavailableto'] = $data->timeavailableto;
}
return $result;
}

View File

@ -1099,12 +1099,8 @@ class mod_data_lib_testcase extends advanced_testcase {
// Decorate action event.
$actionevent = mod_data_core_calendar_provide_event_action($event, $factory);
// Confirm the event was decorated.
$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
$this->assertEquals(get_string('add', 'data'), $actionevent->get_name());
$this->assertInstanceOf('moodle_url', $actionevent->get_url());
$this->assertEquals(1, $actionevent->get_item_count());
$this->assertFalse($actionevent->is_actionable());
// No event on the dashboard if module is closed.
$this->assertNull($actionevent);
}
public function test_data_core_calendar_provide_event_action_open_in_future() {

View File

@ -53,9 +53,9 @@ class mod_feedback_completion extends mod_feedback_structure {
/**
* Constructor
*
* @param stdClass $feedback feedback object, in case of the template
* this is the current feedback the template is accessed from
* @param stdClass $feedback feedback object
* @param cm_info $cm course module object corresponding to the $feedback
* (at least one of $feedback or $cm is required)
* @param int $courseid current course (for site feedbacks only)
* @param bool $iscompleted has feedback been already completed? If yes either completedid or userid must be specified.
* @param int $completedid id in the table feedback_completed, may be omitted if userid is specified
@ -66,17 +66,15 @@ class mod_feedback_completion extends mod_feedback_structure {
*/
public function __construct($feedback, $cm, $courseid, $iscompleted = false, $completedid = null, $userid = null) {
global $DB;
// Make sure courseid is always set for site feedback and never for course feedback.
if ($feedback->course == SITEID) {
$courseid = $courseid ?: SITEID;
} else {
$courseid = 0;
}
parent::__construct($feedback, $cm, $courseid, 0);
// Make sure courseid is always set for site feedback.
if ($this->feedback->course == SITEID && !$this->courseid) {
$this->courseid = SITEID;
}
if ($iscompleted) {
// Retrieve information about the completion.
$this->iscompleted = true;
$params = array('feedback' => $feedback->id);
$params = array('feedback' => $this->feedback->id);
if (!$userid && !$completedid) {
throw new coding_exception('Either $completedid or $userid must be specified for completed feedbacks');
}
@ -713,7 +711,7 @@ class mod_feedback_completion extends mod_feedback_structure {
$this->jumpto = $nextpage;
} else {
$this->save_response();
if (!$this->feedback->page_after_submit) {
if (!$this->get_feedback()->page_after_submit) {
\core\notification::success(get_string('entries_saved', 'feedback'));
}
$this->justcompleted = true;

View File

@ -32,7 +32,10 @@ defined('MOODLE_INTERNAL') || die();
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_feedback_structure {
/** @var stdClass */
/** @var stdClass record from 'feedback' table.
* Reliably has fields: id, course, timeopen, timeclose, anonymous, completionsubmit.
* For full object or to access any other field use $this->get_feedback()
*/
protected $feedback;
/** @var cm_info */
protected $cm;
@ -50,15 +53,31 @@ class mod_feedback_structure {
*
* @param stdClass $feedback feedback object, in case of the template
* this is the current feedback the template is accessed from
* @param cm_info $cm course module object corresponding to the $feedback
* @param stdClass|cm_info $cm course module object corresponding to the $feedback
* (at least one of $feedback or $cm is required)
* @param int $courseid current course (for site feedbacks only)
* @param int $templateid template id if this class represents the template structure
*/
public function __construct($feedback, $cm, $courseid = 0, $templateid = null) {
$this->feedback = $feedback;
$this->cm = $cm;
$this->courseid = ($feedback->course == SITEID) ? $courseid : 0;
if ((empty($feedback->id) || empty($feedback->course)) && (empty($cm->instance) || empty($cm->course))) {
throw new coding_exception('Either $feedback or $cm must be passed to constructor');
}
$this->feedback = $feedback ?: (object)['id' => $cm->instance, 'course' => $cm->course];
$this->cm = ($cm && $cm instanceof cm_info) ? $cm :
get_fast_modinfo($this->feedback->course)->instances['feedback'][$this->feedback->id];
$this->templateid = $templateid;
$this->courseid = ($this->feedback->course == SITEID) ? $courseid : 0;
if (!$feedback) {
// If feedback object was not specified, populate object with fields required for the most of methods.
// These fields were added to course module cache in feedback_get_coursemodule_info().
// Full instance record can be retrieved by calling mod_feedback_structure::get_feedback().
$customdata = ($this->cm->customdata ?: []) + ['timeopen' => 0, 'timeclose' => 0, 'anonymous' => 0];
$this->feedback->timeopen = $customdata['timeopen'];
$this->feedback->timeclose = $customdata['timeclose'];
$this->feedback->anonymous = $customdata['anonymous'];
$this->feedback->completionsubmit = empty($this->cm->customdata['customcompletionrules']['completionsubmit']) ? 0 : 1;
}
}
/**
@ -66,6 +85,11 @@ class mod_feedback_structure {
* @return stdClass
*/
public function get_feedback() {
global $DB;
if (!isset($this->feedback->publish_stats) || !isset($this->feedback->name)) {
// Make sure the full object is retrieved.
$this->feedback = $DB->get_record('feedback', ['id' => $this->feedback->id], '*', MUST_EXIST);
}
return $this->feedback;
}
@ -182,7 +206,7 @@ class mod_feedback_structure {
return true;
}
if (intval($this->feedback->publish_stats) != 1 ||
if (intval($this->get_feedback()->publish_stats) != 1 ||
!has_capability('mod/feedback:viewanalysepage', $context)) {
return false;
}

View File

@ -3428,8 +3428,7 @@ function mod_feedback_core_calendar_is_event_visible(calendar_event $event) {
global $DB;
$cm = get_fast_modinfo($event->courseid)->instances['feedback'][$event->instance];
$feedback = $DB->get_record('feedback', ['id' => $event->instance]);
$feedbackcompletion = new mod_feedback_completion($feedback, $cm, 0);
$feedbackcompletion = new mod_feedback_completion(null, $cm, 0);
// The event is only visible if the user can submit it.
return $feedbackcompletion->can_complete();
@ -3447,26 +3446,21 @@ function mod_feedback_core_calendar_is_event_visible(calendar_event $event) {
*/
function mod_feedback_core_calendar_provide_event_action(calendar_event $event,
\core_calendar\action_factory $factory) {
global $DB;
$cm = get_fast_modinfo($event->courseid)->instances['feedback'][$event->instance];
$feedback = $DB->get_record('feedback', ['id' => $event->instance]);
$feedbackcompletion = new mod_feedback_completion($feedback, $cm, 0);
$feedbackcompletion = new mod_feedback_completion(null, $cm, 0);
if ($feedbackcompletion->is_already_submitted()) {
// There is no action if the user has already submitted the feedback.
if (!empty($cm->customdata['timeclose']) && $cm->customdata['timeclose'] < time()) {
// Feedback is already closed, do not display it even if it was never submitted.
return null;
}
$now = time();
if ($feedback->timeopen && $feedback->timeclose) {
$actionable = ($now >= $feedback->timeopen) && ($now <= $feedback->timeclose);
} else if ($feedback->timeclose) {
$actionable = $now < $feedback->timeclose;
} else if ($feedback->timeopen) {
$actionable = $now >= $feedback->timeopen;
} else {
$actionable = true;
// The feedback is actionable if it does not have timeopen or timeopen is in the past.
$actionable = $feedbackcompletion->is_open();
if ($actionable && $feedbackcompletion->is_already_submitted()) {
// There is no need to display anything if the user has already submitted the feedback.
return null;
}
return $factory->create_instance(
@ -3492,7 +3486,7 @@ function feedback_get_coursemodule_info($coursemodule) {
global $DB;
$dbparams = ['id' => $coursemodule->instance];
$fields = 'id, name, intro, introformat, completionsubmit';
$fields = 'id, name, intro, introformat, completionsubmit, timeopen, timeclose, anonymous';
if (!$feedback = $DB->get_record('feedback', $dbparams, $fields)) {
return false;
}
@ -3509,6 +3503,16 @@ function feedback_get_coursemodule_info($coursemodule) {
if ($coursemodule->completion == COMPLETION_TRACKING_AUTOMATIC) {
$result->customdata['customcompletionrules']['completionsubmit'] = $feedback->completionsubmit;
}
// Populate some other values that can be used in calendar or on dashboard.
if ($feedback->timeopen) {
$result->customdata['timeopen'] = $feedback->timeopen;
}
if ($feedback->timeclose) {
$result->customdata['timeclose'] = $feedback->timeclose;
}
if ($feedback->anonymous) {
$result->customdata['anonymous'] = $feedback->anonymous;
}
return $result;
}

View File

@ -32,6 +32,39 @@ require_once($CFG->dirroot . '/mod/feedback/lib.php');
*/
class mod_feedback_lib_testcase extends advanced_testcase {
public function test_feedback_initialise() {
$this->resetAfterTest();
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$params['course'] = $course->id;
$params['timeopen'] = time() - 5 * MINSECS;
$params['timeclose'] = time() + DAYSECS;
$params['anonymous'] = 1;
$params['intro'] = 'Some introduction text';
$feedback = $this->getDataGenerator()->create_module('feedback', $params);
// Test different ways to construct the structure object.
$pseudocm = get_coursemodule_from_instance('feedback', $feedback->id); // Object similar to cm_info.
$cm = get_fast_modinfo($course)->instances['feedback'][$feedback->id]; // Instance of cm_info.
$constructorparams = [
[$feedback, null],
[null, $pseudocm],
[null, $cm],
[$feedback, $pseudocm],
[$feedback, $cm],
];
foreach ($constructorparams as $params) {
$structure = new mod_feedback_completion($params[0], $params[1], 0);
$this->assertTrue($structure->is_open());
$this->assertTrue($structure->get_cm() instanceof cm_info);
$this->assertEquals($feedback->cmid, $structure->get_cm()->id);
$this->assertEquals($feedback->intro, $structure->get_feedback()->intro);
}
}
/**
* Tests for mod_feedback_refresh_events.
*/
@ -175,11 +208,8 @@ class mod_feedback_lib_testcase extends advanced_testcase {
$factory = new \core_calendar\action_factory();
$actionevent = mod_feedback_core_calendar_provide_event_action($event, $factory);
$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
$this->assertEquals(get_string('answerquestions', 'feedback'), $actionevent->get_name());
$this->assertInstanceOf('moodle_url', $actionevent->get_url());
$this->assertEquals(1, $actionevent->get_item_count());
$this->assertFalse($actionevent->is_actionable());
// No event on the dashboard if feedback is closed.
$this->assertNull($actionevent);
}
/**

View File

@ -790,9 +790,7 @@ function mod_folder_core_calendar_provide_event_action(calendar_event $event,
\core_calendar\action_factory $factory) {
$cm = get_fast_modinfo($event->courseid)->instances['folder'][$event->instance];
$course = new stdClass();
$course->id = $event->courseid;
$completion = new \completion_info($course);
$completion = new \completion_info($cm->get_course());
$completiondata = $completion->get_data($cm, false);

View File

@ -8211,9 +8211,7 @@ function mod_forum_core_calendar_provide_event_action(calendar_event $event,
return null;
}
$course = new stdClass();
$course->id = $event->courseid;
$completion = new \completion_info($course);
$completion = new \completion_info($cm->get_course());
$completiondata = $completion->get_data($cm, false);

View File

@ -4187,9 +4187,7 @@ function mod_glossary_core_calendar_provide_event_action(calendar_event $event,
\core_calendar\action_factory $factory) {
$cm = get_fast_modinfo($event->courseid)->instances['glossary'][$event->instance];
$course = new stdClass();
$course->id = $event->courseid;
$completion = new \completion_info($course);
$completion = new \completion_info($cm->get_course());
$completiondata = $completion->get_data($cm, false);

View File

@ -487,9 +487,7 @@ function mod_imscp_core_calendar_provide_event_action(calendar_event $event,
\core_calendar\action_factory $factory) {
$cm = get_fast_modinfo($event->courseid)->instances['imscp'][$event->instance];
$course = new stdClass();
$course->id = $event->courseid;
$completion = new \completion_info($course);
$completion = new \completion_info($cm->get_course());
$completiondata = $completion->get_data($cm, false);

View File

@ -364,9 +364,7 @@ function mod_label_core_calendar_provide_event_action(calendar_event $event,
\core_calendar\action_factory $factory) {
$cm = get_fast_modinfo($event->courseid)->instances['label'][$event->instance];
$course = new stdClass();
$course->id = $event->courseid;
$completion = new \completion_info($course);
$completion = new \completion_info($cm->get_course());
$completiondata = $completion->get_data($cm, false);

View File

@ -662,9 +662,7 @@ function mod_lti_core_calendar_provide_event_action(calendar_event $event,
\core_calendar\action_factory $factory) {
$cm = get_fast_modinfo($event->courseid)->instances['lti'][$event->instance];
$course = new stdClass();
$course->id = $event->courseid;
$completion = new \completion_info($course);
$completion = new \completion_info($cm->get_course());
$completiondata = $completion->get_data($cm, false);

View File

@ -551,9 +551,7 @@ function mod_page_core_calendar_provide_event_action(calendar_event $event,
\core_calendar\action_factory $factory) {
$cm = get_fast_modinfo($event->courseid)->instances['page'][$event->instance];
$course = new stdClass();
$course->id = $event->courseid;
$completion = new \completion_info($course);
$completion = new \completion_info($cm->get_course());
$completiondata = $completion->get_data($cm, false);

View File

@ -566,9 +566,7 @@ function mod_resource_core_calendar_provide_event_action(calendar_event $event,
\core_calendar\action_factory $factory) {
$cm = get_fast_modinfo($event->courseid)->instances['resource'][$event->instance];
$course = new stdClass();
$course->id = $event->courseid;
$completion = new \completion_info($course);
$completion = new \completion_info($cm->get_course());
$completiondata = $completion->get_data($cm, false);

View File

@ -1627,12 +1627,21 @@ function scorm_refresh_events($courseid = 0) {
*/
function mod_scorm_core_calendar_provide_event_action(calendar_event $event,
\core_calendar\action_factory $factory) {
global $CFG, $DB;
global $CFG;
require_once($CFG->dirroot . '/mod/scorm/locallib.php');
$cm = get_fast_modinfo($event->courseid)->instances['scorm'][$event->instance];
$scorm = $DB->get_record('scorm', array('id' => $event->instance));
if (!empty($cm->customdata['timeclose']) && $cm->customdata['timeclose'] < time()) {
// The scorm has closed so the user can no longer submit anything.
return null;
}
// Restore scorm object from cached values in $cm, we only need id, timeclose and timeopen.
$customdata = $cm->customdata ?: [];
$customdata['id'] = $cm->instance;
$scorm = (object)($customdata + ['timeclose' => 0, 'timeopen' => 0]);
// Check that the SCORM activity is open.
list($actionable, $warnings) = scorm_get_availability_status($scorm);
@ -1660,7 +1669,8 @@ function scorm_get_coursemodule_info($coursemodule) {
global $DB;
$dbparams = ['id' => $coursemodule->instance];
$fields = 'id, name, intro, introformat, completionstatusrequired, completionscorerequired, completionstatusallscos';
$fields = 'id, name, intro, introformat, completionstatusrequired, completionscorerequired, completionstatusallscos, '.
'timeopen, timeclose';
if (!$scorm = $DB->get_record('scorm', $dbparams, $fields)) {
return false;
}
@ -1679,6 +1689,13 @@ function scorm_get_coursemodule_info($coursemodule) {
$result->customdata['customcompletionrules']['completionscorerequired'] = $scorm->completionscorerequired;
$result->customdata['customcompletionrules']['completionstatusallscos'] = $scorm->completionstatusallscos;
}
// Populate some other values that can be used in calendar or on dashboard.
if ($scorm->timeopen) {
$result->customdata['timeopen'] = $scorm->timeopen;
}
if ($scorm->timeclose) {
$result->customdata['timeclose'] = $scorm->timeclose;
}
return $result;
}

View File

@ -242,12 +242,8 @@ class mod_scorm_lib_testcase extends externallib_advanced_testcase {
// Decorate action event.
$actionevent = mod_scorm_core_calendar_provide_event_action($event, $factory);
// Confirm the event was decorated.
$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
$this->assertEquals(get_string('enter', 'scorm'), $actionevent->get_name());
$this->assertInstanceOf('moodle_url', $actionevent->get_url());
$this->assertEquals(1, $actionevent->get_item_count());
$this->assertFalse($actionevent->is_actionable());
// No event on the dashboard if module is closed.
$this->assertNull($actionevent);
}
public function test_scorm_core_calendar_provide_event_action_open_in_future() {

View File

@ -1128,9 +1128,7 @@ function mod_survey_core_calendar_provide_event_action(calendar_event $event,
return null;
}
$course = new stdClass();
$course->id = $event->courseid;
$completion = new \completion_info($course);
$completion = new \completion_info($cm->get_course());
$completiondata = $completion->get_data($cm, false);

View File

@ -389,9 +389,7 @@ function mod_url_core_calendar_provide_event_action(calendar_event $event,
\core_calendar\action_factory $factory) {
$cm = get_fast_modinfo($event->courseid)->instances['url'][$event->instance];
$course = new stdClass();
$course->id = $event->courseid;
$completion = new \completion_info($course);
$completion = new \completion_info($cm->get_course());
$completiondata = $completion->get_data($cm, false);

View File

@ -815,9 +815,7 @@ function mod_wiki_core_calendar_provide_event_action(calendar_event $event,
\core_calendar\action_factory $factory) {
$cm = get_fast_modinfo($event->courseid)->instances['wiki'][$event->instance];
$course = new stdClass();
$course->id = $event->courseid;
$completion = new \completion_info($course);
$completion = new \completion_info($cm->get_course());
$completiondata = $completion->get_data($cm, false);