PageRenderTime 53ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/choice/lib.php

https://bitbucket.org/moodle/moodle
PHP | 1373 lines | 775 code | 180 blank | 418 comment | 162 complexity | ceec188e6c217dcc00ebe804f677bd08 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * @package mod_choice
  18. * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
  19. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  20. */
  21. defined('MOODLE_INTERNAL') || die();
  22. /** @global int $CHOICE_COLUMN_HEIGHT */
  23. global $CHOICE_COLUMN_HEIGHT;
  24. $CHOICE_COLUMN_HEIGHT = 300;
  25. /** @global int $CHOICE_COLUMN_WIDTH */
  26. global $CHOICE_COLUMN_WIDTH;
  27. $CHOICE_COLUMN_WIDTH = 300;
  28. define('CHOICE_PUBLISH_ANONYMOUS', '0');
  29. define('CHOICE_PUBLISH_NAMES', '1');
  30. define('CHOICE_SHOWRESULTS_NOT', '0');
  31. define('CHOICE_SHOWRESULTS_AFTER_ANSWER', '1');
  32. define('CHOICE_SHOWRESULTS_AFTER_CLOSE', '2');
  33. define('CHOICE_SHOWRESULTS_ALWAYS', '3');
  34. define('CHOICE_DISPLAY_HORIZONTAL', '0');
  35. define('CHOICE_DISPLAY_VERTICAL', '1');
  36. define('CHOICE_EVENT_TYPE_OPEN', 'open');
  37. define('CHOICE_EVENT_TYPE_CLOSE', 'close');
  38. /** @global array $CHOICE_PUBLISH */
  39. global $CHOICE_PUBLISH;
  40. $CHOICE_PUBLISH = array (CHOICE_PUBLISH_ANONYMOUS => get_string('publishanonymous', 'choice'),
  41. CHOICE_PUBLISH_NAMES => get_string('publishnames', 'choice'));
  42. /** @global array $CHOICE_SHOWRESULTS */
  43. global $CHOICE_SHOWRESULTS;
  44. $CHOICE_SHOWRESULTS = array (CHOICE_SHOWRESULTS_NOT => get_string('publishnot', 'choice'),
  45. CHOICE_SHOWRESULTS_AFTER_ANSWER => get_string('publishafteranswer', 'choice'),
  46. CHOICE_SHOWRESULTS_AFTER_CLOSE => get_string('publishafterclose', 'choice'),
  47. CHOICE_SHOWRESULTS_ALWAYS => get_string('publishalways', 'choice'));
  48. /** @global array $CHOICE_DISPLAY */
  49. global $CHOICE_DISPLAY;
  50. $CHOICE_DISPLAY = array (CHOICE_DISPLAY_HORIZONTAL => get_string('displayhorizontal', 'choice'),
  51. CHOICE_DISPLAY_VERTICAL => get_string('displayvertical','choice'));
  52. require_once(__DIR__ . '/deprecatedlib.php');
  53. /// Standard functions /////////////////////////////////////////////////////////
  54. /**
  55. * @global object
  56. * @param object $course
  57. * @param object $user
  58. * @param object $mod
  59. * @param object $choice
  60. * @return object|null
  61. */
  62. function choice_user_outline($course, $user, $mod, $choice) {
  63. global $DB;
  64. if ($answer = $DB->get_record('choice_answers', array('choiceid' => $choice->id, 'userid' => $user->id))) {
  65. $result = new stdClass();
  66. $result->info = "'".format_string(choice_get_option_text($choice, $answer->optionid))."'";
  67. $result->time = $answer->timemodified;
  68. return $result;
  69. }
  70. return NULL;
  71. }
  72. /**
  73. * Callback for the "Complete" report - prints the activity summary for the given user
  74. *
  75. * @param object $course
  76. * @param object $user
  77. * @param object $mod
  78. * @param object $choice
  79. */
  80. function choice_user_complete($course, $user, $mod, $choice) {
  81. global $DB;
  82. if ($answers = $DB->get_records('choice_answers', array("choiceid" => $choice->id, "userid" => $user->id))) {
  83. $info = [];
  84. foreach ($answers as $answer) {
  85. $info[] = "'" . format_string(choice_get_option_text($choice, $answer->optionid)) . "'";
  86. }
  87. core_collator::asort($info);
  88. echo get_string("answered", "choice") . ": ". join(', ', $info) . ". " .
  89. get_string("updated", '', userdate($answer->timemodified));
  90. } else {
  91. print_string("notanswered", "choice");
  92. }
  93. }
  94. /**
  95. * Given an object containing all the necessary data,
  96. * (defined by the form in mod_form.php) this function
  97. * will create a new instance and return the id number
  98. * of the new instance.
  99. *
  100. * @global object
  101. * @param object $choice
  102. * @return int
  103. */
  104. function choice_add_instance($choice) {
  105. global $DB, $CFG;
  106. require_once($CFG->dirroot.'/mod/choice/locallib.php');
  107. $choice->timemodified = time();
  108. //insert answers
  109. $choice->id = $DB->insert_record("choice", $choice);
  110. foreach ($choice->option as $key => $value) {
  111. $value = trim($value);
  112. if (isset($value) && $value <> '') {
  113. $option = new stdClass();
  114. $option->text = $value;
  115. $option->choiceid = $choice->id;
  116. if (isset($choice->limit[$key])) {
  117. $option->maxanswers = $choice->limit[$key];
  118. }
  119. $option->timemodified = time();
  120. $DB->insert_record("choice_options", $option);
  121. }
  122. }
  123. // Add calendar events if necessary.
  124. choice_set_events($choice);
  125. if (!empty($choice->completionexpected)) {
  126. \core_completion\api::update_completion_date_event($choice->coursemodule, 'choice', $choice->id,
  127. $choice->completionexpected);
  128. }
  129. return $choice->id;
  130. }
  131. /**
  132. * Given an object containing all the necessary data,
  133. * (defined by the form in mod_form.php) this function
  134. * will update an existing instance with new data.
  135. *
  136. * @global object
  137. * @param object $choice
  138. * @return bool
  139. */
  140. function choice_update_instance($choice) {
  141. global $DB, $CFG;
  142. require_once($CFG->dirroot.'/mod/choice/locallib.php');
  143. $choice->id = $choice->instance;
  144. $choice->timemodified = time();
  145. //update, delete or insert answers
  146. foreach ($choice->option as $key => $value) {
  147. $value = trim($value);
  148. $option = new stdClass();
  149. $option->text = $value;
  150. $option->choiceid = $choice->id;
  151. if (isset($choice->limit[$key])) {
  152. $option->maxanswers = $choice->limit[$key];
  153. }
  154. $option->timemodified = time();
  155. if (isset($choice->optionid[$key]) && !empty($choice->optionid[$key])){//existing choice record
  156. $option->id=$choice->optionid[$key];
  157. if (isset($value) && $value <> '') {
  158. $DB->update_record("choice_options", $option);
  159. } else {
  160. // Remove the empty (unused) option.
  161. $DB->delete_records("choice_options", array("id" => $option->id));
  162. // Delete any answers associated with this option.
  163. $DB->delete_records("choice_answers", array("choiceid" => $choice->id, "optionid" => $option->id));
  164. }
  165. } else {
  166. if (isset($value) && $value <> '') {
  167. $DB->insert_record("choice_options", $option);
  168. }
  169. }
  170. }
  171. // Add calendar events if necessary.
  172. choice_set_events($choice);
  173. $completionexpected = (!empty($choice->completionexpected)) ? $choice->completionexpected : null;
  174. \core_completion\api::update_completion_date_event($choice->coursemodule, 'choice', $choice->id, $completionexpected);
  175. return $DB->update_record('choice', $choice);
  176. }
  177. /**
  178. * @global object
  179. * @param object $choice
  180. * @param object $user
  181. * @param object $coursemodule
  182. * @param array $allresponses
  183. * @return array
  184. */
  185. function choice_prepare_options($choice, $user, $coursemodule, $allresponses) {
  186. global $DB;
  187. $cdisplay = array('options'=>array());
  188. $cdisplay['limitanswers'] = $choice->limitanswers;
  189. $cdisplay['showavailable'] = $choice->showavailable;
  190. $context = context_module::instance($coursemodule->id);
  191. foreach ($choice->option as $optionid => $text) {
  192. if (isset($text)) { //make sure there are no dud entries in the db with blank text values.
  193. $option = new stdClass;
  194. $option->attributes = new stdClass;
  195. $option->attributes->value = $optionid;
  196. $option->text = format_string($text);
  197. $option->maxanswers = $choice->maxanswers[$optionid];
  198. $option->displaylayout = $choice->display;
  199. if (isset($allresponses[$optionid])) {
  200. $option->countanswers = count($allresponses[$optionid]);
  201. } else {
  202. $option->countanswers = 0;
  203. }
  204. if ($DB->record_exists('choice_answers', array('choiceid' => $choice->id, 'userid' => $user->id, 'optionid' => $optionid))) {
  205. $option->attributes->checked = true;
  206. }
  207. if ( $choice->limitanswers && ($option->countanswers >= $option->maxanswers) && empty($option->attributes->checked)) {
  208. $option->attributes->disabled = true;
  209. }
  210. $cdisplay['options'][] = $option;
  211. }
  212. }
  213. $cdisplay['hascapability'] = is_enrolled($context, NULL, 'mod/choice:choose'); //only enrolled users are allowed to make a choice
  214. if ($choice->allowupdate && $DB->record_exists('choice_answers', array('choiceid'=> $choice->id, 'userid'=> $user->id))) {
  215. $cdisplay['allowupdate'] = true;
  216. }
  217. if ($choice->showpreview && $choice->timeopen > time()) {
  218. $cdisplay['previewonly'] = true;
  219. }
  220. return $cdisplay;
  221. }
  222. /**
  223. * Modifies responses of other users adding the option $newoptionid to them
  224. *
  225. * @param array $userids list of users to add option to (must be users without any answers yet)
  226. * @param array $answerids list of existing attempt ids of users (will be either appended or
  227. * substituted with the newoptionid, depending on $choice->allowmultiple)
  228. * @param int $newoptionid
  229. * @param stdClass $choice choice object, result of {@link choice_get_choice()}
  230. * @param stdClass $cm
  231. * @param stdClass $course
  232. */
  233. function choice_modify_responses($userids, $answerids, $newoptionid, $choice, $cm, $course) {
  234. // Get all existing responses and the list of non-respondents.
  235. $groupmode = groups_get_activity_groupmode($cm);
  236. $onlyactive = $choice->includeinactive ? false : true;
  237. $allresponses = choice_get_response_data($choice, $cm, $groupmode, $onlyactive);
  238. // Check that the option value is valid.
  239. if (!$newoptionid || !isset($choice->option[$newoptionid])) {
  240. return;
  241. }
  242. // First add responses for users who did not make any choice yet.
  243. foreach ($userids as $userid) {
  244. if (isset($allresponses[0][$userid])) {
  245. choice_user_submit_response($newoptionid, $choice, $userid, $course, $cm);
  246. }
  247. }
  248. // Create the list of all options already selected by each user.
  249. $optionsbyuser = []; // Mapping userid=>array of chosen choice options.
  250. $usersbyanswer = []; // Mapping answerid=>userid (which answer belongs to each user).
  251. foreach ($allresponses as $optionid => $responses) {
  252. if ($optionid > 0) {
  253. foreach ($responses as $userid => $userresponse) {
  254. $optionsbyuser += [$userid => []];
  255. $optionsbyuser[$userid][] = $optionid;
  256. $usersbyanswer[$userresponse->answerid] = $userid;
  257. }
  258. }
  259. }
  260. // Go through the list of submitted attemptids and find which users answers need to be updated.
  261. foreach ($answerids as $answerid) {
  262. if (isset($usersbyanswer[$answerid])) {
  263. $userid = $usersbyanswer[$answerid];
  264. if (!in_array($newoptionid, $optionsbyuser[$userid])) {
  265. $options = $choice->allowmultiple ?
  266. array_merge($optionsbyuser[$userid], [$newoptionid]) : $newoptionid;
  267. choice_user_submit_response($options, $choice, $userid, $course, $cm);
  268. }
  269. }
  270. }
  271. }
  272. /**
  273. * Process user submitted answers for a choice,
  274. * and either updating them or saving new answers.
  275. *
  276. * @param int|array $formanswer the id(s) of the user submitted choice options.
  277. * @param object $choice the selected choice.
  278. * @param int $userid user identifier.
  279. * @param object $course current course.
  280. * @param object $cm course context.
  281. * @return void
  282. */
  283. function choice_user_submit_response($formanswer, $choice, $userid, $course, $cm) {
  284. global $DB, $CFG, $USER;
  285. require_once($CFG->libdir.'/completionlib.php');
  286. $continueurl = new moodle_url('/mod/choice/view.php', array('id' => $cm->id));
  287. if (empty($formanswer)) {
  288. print_error('atleastoneoption', 'choice', $continueurl);
  289. }
  290. if (is_array($formanswer)) {
  291. if (!$choice->allowmultiple) {
  292. print_error('multiplenotallowederror', 'choice', $continueurl);
  293. }
  294. $formanswers = $formanswer;
  295. } else {
  296. $formanswers = array($formanswer);
  297. }
  298. $options = $DB->get_records('choice_options', array('choiceid' => $choice->id), '', 'id');
  299. foreach ($formanswers as $key => $val) {
  300. if (!isset($options[$val])) {
  301. print_error('cannotsubmit', 'choice', $continueurl);
  302. }
  303. }
  304. // Start lock to prevent synchronous access to the same data
  305. // before it's updated, if using limits.
  306. if ($choice->limitanswers) {
  307. $timeout = 10;
  308. $locktype = 'mod_choice_choice_user_submit_response';
  309. // Limiting access to this choice.
  310. $resouce = 'choiceid:' . $choice->id;
  311. $lockfactory = \core\lock\lock_config::get_lock_factory($locktype);
  312. // Opening the lock.
  313. $choicelock = $lockfactory->get_lock($resouce, $timeout, MINSECS);
  314. if (!$choicelock) {
  315. print_error('cannotsubmit', 'choice', $continueurl);
  316. }
  317. }
  318. $current = $DB->get_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $userid));
  319. // Array containing [answerid => optionid] mapping.
  320. $existinganswers = array_map(function($answer) {
  321. return $answer->optionid;
  322. }, $current);
  323. $context = context_module::instance($cm->id);
  324. $choicesexceeded = false;
  325. $countanswers = array();
  326. foreach ($formanswers as $val) {
  327. $countanswers[$val] = 0;
  328. }
  329. if($choice->limitanswers) {
  330. // Find out whether groups are being used and enabled
  331. if (groups_get_activity_groupmode($cm) > 0) {
  332. $currentgroup = groups_get_activity_group($cm);
  333. } else {
  334. $currentgroup = 0;
  335. }
  336. list ($insql, $params) = $DB->get_in_or_equal($formanswers, SQL_PARAMS_NAMED);
  337. if($currentgroup) {
  338. // If groups are being used, retrieve responses only for users in
  339. // current group
  340. global $CFG;
  341. $params['groupid'] = $currentgroup;
  342. $sql = "SELECT ca.*
  343. FROM {choice_answers} ca
  344. INNER JOIN {groups_members} gm ON ca.userid=gm.userid
  345. WHERE optionid $insql
  346. AND gm.groupid= :groupid";
  347. } else {
  348. // Groups are not used, retrieve all answers for this option ID
  349. $sql = "SELECT ca.*
  350. FROM {choice_answers} ca
  351. WHERE optionid $insql";
  352. }
  353. $answers = $DB->get_records_sql($sql, $params);
  354. if ($answers) {
  355. foreach ($answers as $a) { //only return enrolled users.
  356. if (is_enrolled($context, $a->userid, 'mod/choice:choose')) {
  357. $countanswers[$a->optionid]++;
  358. }
  359. }
  360. }
  361. foreach ($countanswers as $opt => $count) {
  362. // Ignore the user's existing answers when checking whether an answer count has been exceeded.
  363. // A user may wish to update their response with an additional choice option and shouldn't be competing with themself!
  364. if (in_array($opt, $existinganswers)) {
  365. continue;
  366. }
  367. if ($count >= $choice->maxanswers[$opt]) {
  368. $choicesexceeded = true;
  369. break;
  370. }
  371. }
  372. }
  373. // Check the user hasn't exceeded the maximum selections for the choice(s) they have selected.
  374. $answersnapshots = array();
  375. $deletedanswersnapshots = array();
  376. if (!($choice->limitanswers && $choicesexceeded)) {
  377. if ($current) {
  378. // Update an existing answer.
  379. foreach ($current as $c) {
  380. if (in_array($c->optionid, $formanswers)) {
  381. $DB->set_field('choice_answers', 'timemodified', time(), array('id' => $c->id));
  382. } else {
  383. $deletedanswersnapshots[] = $c;
  384. $DB->delete_records('choice_answers', array('id' => $c->id));
  385. }
  386. }
  387. // Add new ones.
  388. foreach ($formanswers as $f) {
  389. if (!in_array($f, $existinganswers)) {
  390. $newanswer = new stdClass();
  391. $newanswer->optionid = $f;
  392. $newanswer->choiceid = $choice->id;
  393. $newanswer->userid = $userid;
  394. $newanswer->timemodified = time();
  395. $newanswer->id = $DB->insert_record("choice_answers", $newanswer);
  396. $answersnapshots[] = $newanswer;
  397. }
  398. }
  399. } else {
  400. // Add new answer.
  401. foreach ($formanswers as $answer) {
  402. $newanswer = new stdClass();
  403. $newanswer->choiceid = $choice->id;
  404. $newanswer->userid = $userid;
  405. $newanswer->optionid = $answer;
  406. $newanswer->timemodified = time();
  407. $newanswer->id = $DB->insert_record("choice_answers", $newanswer);
  408. $answersnapshots[] = $newanswer;
  409. }
  410. // Update completion state
  411. $completion = new completion_info($course);
  412. if ($completion->is_enabled($cm) && $choice->completionsubmit) {
  413. $completion->update_state($cm, COMPLETION_COMPLETE);
  414. }
  415. }
  416. } else {
  417. // This is a choice with limited options, and one of the options selected has just run over its limit.
  418. $choicelock->release();
  419. print_error('choicefull', 'choice', $continueurl);
  420. }
  421. // Release lock.
  422. if (isset($choicelock)) {
  423. $choicelock->release();
  424. }
  425. // Trigger events.
  426. foreach ($deletedanswersnapshots as $answer) {
  427. \mod_choice\event\answer_deleted::create_from_object($answer, $choice, $cm, $course)->trigger();
  428. }
  429. foreach ($answersnapshots as $answer) {
  430. \mod_choice\event\answer_created::create_from_object($answer, $choice, $cm, $course)->trigger();
  431. }
  432. }
  433. /**
  434. * @param array $user
  435. * @param object $cm
  436. * @return void Output is echo'd
  437. */
  438. function choice_show_reportlink($user, $cm) {
  439. $userschosen = array();
  440. foreach($user as $optionid => $userlist) {
  441. if ($optionid) {
  442. $userschosen = array_merge($userschosen, array_keys($userlist));
  443. }
  444. }
  445. $responsecount = count(array_unique($userschosen));
  446. echo '<div class="reportlink">';
  447. echo "<a href=\"report.php?id=$cm->id\">".get_string("viewallresponses", "choice", $responsecount)."</a>";
  448. echo '</div>';
  449. }
  450. /**
  451. * @global object
  452. * @param object $choice
  453. * @param object $course
  454. * @param object $coursemodule
  455. * @param array $allresponses
  456. * * @param bool $allresponses
  457. * @return object
  458. */
  459. function prepare_choice_show_results($choice, $course, $cm, $allresponses) {
  460. global $OUTPUT;
  461. $display = clone($choice);
  462. $display->coursemoduleid = $cm->id;
  463. $display->courseid = $course->id;
  464. if (!empty($choice->showunanswered)) {
  465. $choice->option[0] = get_string('notanswered', 'choice');
  466. $choice->maxanswers[0] = 0;
  467. }
  468. // Remove from the list of non-respondents the users who do not have access to this activity.
  469. if (!empty($display->showunanswered) && $allresponses[0]) {
  470. $info = new \core_availability\info_module(cm_info::create($cm));
  471. $allresponses[0] = $info->filter_user_list($allresponses[0]);
  472. }
  473. //overwrite options value;
  474. $display->options = array();
  475. $allusers = [];
  476. foreach ($choice->option as $optionid => $optiontext) {
  477. $display->options[$optionid] = new stdClass;
  478. $display->options[$optionid]->text = format_string($optiontext, true,
  479. ['context' => context_module::instance($cm->id)]);
  480. $display->options[$optionid]->maxanswer = $choice->maxanswers[$optionid];
  481. if (array_key_exists($optionid, $allresponses)) {
  482. $display->options[$optionid]->user = $allresponses[$optionid];
  483. $allusers = array_merge($allusers, array_keys($allresponses[$optionid]));
  484. }
  485. }
  486. unset($display->option);
  487. unset($display->maxanswers);
  488. $display->numberofuser = count(array_unique($allusers));
  489. $context = context_module::instance($cm->id);
  490. $display->viewresponsecapability = has_capability('mod/choice:readresponses', $context);
  491. $display->deleterepsonsecapability = has_capability('mod/choice:deleteresponses',$context);
  492. $display->fullnamecapability = has_capability('moodle/site:viewfullnames', $context);
  493. if (empty($allresponses)) {
  494. echo $OUTPUT->heading(get_string("nousersyet"), 3, null);
  495. return false;
  496. }
  497. return $display;
  498. }
  499. /**
  500. * @global object
  501. * @param array $attemptids
  502. * @param object $choice Choice main table row
  503. * @param object $cm Course-module object
  504. * @param object $course Course object
  505. * @return bool
  506. */
  507. function choice_delete_responses($attemptids, $choice, $cm, $course) {
  508. global $DB, $CFG, $USER;
  509. require_once($CFG->libdir.'/completionlib.php');
  510. if(!is_array($attemptids) || empty($attemptids)) {
  511. return false;
  512. }
  513. foreach($attemptids as $num => $attemptid) {
  514. if(empty($attemptid)) {
  515. unset($attemptids[$num]);
  516. }
  517. }
  518. $completion = new completion_info($course);
  519. foreach($attemptids as $attemptid) {
  520. if ($todelete = $DB->get_record('choice_answers', array('choiceid' => $choice->id, 'id' => $attemptid))) {
  521. // Trigger the event answer deleted.
  522. \mod_choice\event\answer_deleted::create_from_object($todelete, $choice, $cm, $course)->trigger();
  523. $DB->delete_records('choice_answers', array('choiceid' => $choice->id, 'id' => $attemptid));
  524. }
  525. }
  526. // Update completion state.
  527. if ($completion->is_enabled($cm) && $choice->completionsubmit) {
  528. $completion->update_state($cm, COMPLETION_INCOMPLETE);
  529. }
  530. return true;
  531. }
  532. /**
  533. * Given an ID of an instance of this module,
  534. * this function will permanently delete the instance
  535. * and any data that depends on it.
  536. *
  537. * @global object
  538. * @param int $id
  539. * @return bool
  540. */
  541. function choice_delete_instance($id) {
  542. global $DB;
  543. if (! $choice = $DB->get_record("choice", array("id"=>"$id"))) {
  544. return false;
  545. }
  546. $result = true;
  547. if (! $DB->delete_records("choice_answers", array("choiceid"=>"$choice->id"))) {
  548. $result = false;
  549. }
  550. if (! $DB->delete_records("choice_options", array("choiceid"=>"$choice->id"))) {
  551. $result = false;
  552. }
  553. if (! $DB->delete_records("choice", array("id"=>"$choice->id"))) {
  554. $result = false;
  555. }
  556. // Remove old calendar events.
  557. if (! $DB->delete_records('event', array('modulename' => 'choice', 'instance' => $choice->id))) {
  558. $result = false;
  559. }
  560. return $result;
  561. }
  562. /**
  563. * Returns text string which is the answer that matches the id
  564. *
  565. * @global object
  566. * @param object $choice
  567. * @param int $id
  568. * @return string
  569. */
  570. function choice_get_option_text($choice, $id) {
  571. global $DB;
  572. if ($result = $DB->get_record("choice_options", array("id" => $id))) {
  573. return $result->text;
  574. } else {
  575. return get_string("notanswered", "choice");
  576. }
  577. }
  578. /**
  579. * Gets a full choice record
  580. *
  581. * @global object
  582. * @param int $choiceid
  583. * @return object|bool The choice or false
  584. */
  585. function choice_get_choice($choiceid) {
  586. global $DB;
  587. if ($choice = $DB->get_record("choice", array("id" => $choiceid))) {
  588. if ($options = $DB->get_records("choice_options", array("choiceid" => $choiceid), "id")) {
  589. foreach ($options as $option) {
  590. $choice->option[$option->id] = $option->text;
  591. $choice->maxanswers[$option->id] = $option->maxanswers;
  592. }
  593. return $choice;
  594. }
  595. }
  596. return false;
  597. }
  598. /**
  599. * List the actions that correspond to a view of this module.
  600. * This is used by the participation report.
  601. *
  602. * Note: This is not used by new logging system. Event with
  603. * crud = 'r' and edulevel = LEVEL_PARTICIPATING will
  604. * be considered as view action.
  605. *
  606. * @return array
  607. */
  608. function choice_get_view_actions() {
  609. return array('view','view all','report');
  610. }
  611. /**
  612. * List the actions that correspond to a post of this module.
  613. * This is used by the participation report.
  614. *
  615. * Note: This is not used by new logging system. Event with
  616. * crud = ('c' || 'u' || 'd') and edulevel = LEVEL_PARTICIPATING
  617. * will be considered as post action.
  618. *
  619. * @return array
  620. */
  621. function choice_get_post_actions() {
  622. return array('choose','choose again');
  623. }
  624. /**
  625. * Implementation of the function for printing the form elements that control
  626. * whether the course reset functionality affects the choice.
  627. *
  628. * @param object $mform form passed by reference
  629. */
  630. function choice_reset_course_form_definition(&$mform) {
  631. $mform->addElement('header', 'choiceheader', get_string('modulenameplural', 'choice'));
  632. $mform->addElement('advcheckbox', 'reset_choice', get_string('removeresponses','choice'));
  633. }
  634. /**
  635. * Course reset form defaults.
  636. *
  637. * @return array
  638. */
  639. function choice_reset_course_form_defaults($course) {
  640. return array('reset_choice'=>1);
  641. }
  642. /**
  643. * Actual implementation of the reset course functionality, delete all the
  644. * choice responses for course $data->courseid.
  645. *
  646. * @global object
  647. * @global object
  648. * @param object $data the data submitted from the reset course.
  649. * @return array status array
  650. */
  651. function choice_reset_userdata($data) {
  652. global $CFG, $DB;
  653. $componentstr = get_string('modulenameplural', 'choice');
  654. $status = array();
  655. if (!empty($data->reset_choice)) {
  656. $choicessql = "SELECT ch.id
  657. FROM {choice} ch
  658. WHERE ch.course=?";
  659. $DB->delete_records_select('choice_answers', "choiceid IN ($choicessql)", array($data->courseid));
  660. $status[] = array('component'=>$componentstr, 'item'=>get_string('removeresponses', 'choice'), 'error'=>false);
  661. }
  662. /// updating dates - shift may be negative too
  663. if ($data->timeshift) {
  664. // Any changes to the list of dates that needs to be rolled should be same during course restore and course reset.
  665. // See MDL-9367.
  666. shift_course_mod_dates('choice', array('timeopen', 'timeclose'), $data->timeshift, $data->courseid);
  667. $status[] = array('component'=>$componentstr, 'item'=>get_string('datechanged'), 'error'=>false);
  668. }
  669. return $status;
  670. }
  671. /**
  672. * @global object
  673. * @global object
  674. * @global object
  675. * @uses CONTEXT_MODULE
  676. * @param object $choice
  677. * @param object $cm
  678. * @param int $groupmode
  679. * @param bool $onlyactive Whether to get response data for active users only.
  680. * @return array
  681. */
  682. function choice_get_response_data($choice, $cm, $groupmode, $onlyactive) {
  683. global $CFG, $USER, $DB;
  684. $context = context_module::instance($cm->id);
  685. /// Get the current group
  686. if ($groupmode > 0) {
  687. $currentgroup = groups_get_activity_group($cm);
  688. } else {
  689. $currentgroup = 0;
  690. }
  691. /// Initialise the returned array, which is a matrix: $allresponses[responseid][userid] = responseobject
  692. $allresponses = array();
  693. /// First get all the users who have access here
  694. /// To start with we assume they are all "unanswered" then move them later
  695. // TODO Does not support custom user profile fields (MDL-70456).
  696. $userfieldsapi = \core_user\fields::for_identity($context, false)->with_userpic();
  697. $userfields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
  698. $allresponses[0] = get_enrolled_users($context, 'mod/choice:choose', $currentgroup,
  699. $userfields, null, 0, 0, $onlyactive);
  700. /// Get all the recorded responses for this choice
  701. $rawresponses = $DB->get_records('choice_answers', array('choiceid' => $choice->id));
  702. /// Use the responses to move users into the correct column
  703. if ($rawresponses) {
  704. $answeredusers = array();
  705. foreach ($rawresponses as $response) {
  706. if (isset($allresponses[0][$response->userid])) { // This person is enrolled and in correct group
  707. $allresponses[0][$response->userid]->timemodified = $response->timemodified;
  708. $allresponses[$response->optionid][$response->userid] = clone($allresponses[0][$response->userid]);
  709. $allresponses[$response->optionid][$response->userid]->answerid = $response->id;
  710. $answeredusers[] = $response->userid;
  711. }
  712. }
  713. foreach ($answeredusers as $answereduser) {
  714. unset($allresponses[0][$answereduser]);
  715. }
  716. }
  717. return $allresponses;
  718. }
  719. /**
  720. * @uses FEATURE_GROUPS
  721. * @uses FEATURE_GROUPINGS
  722. * @uses FEATURE_MOD_INTRO
  723. * @uses FEATURE_COMPLETION_TRACKS_VIEWS
  724. * @uses FEATURE_GRADE_HAS_GRADE
  725. * @uses FEATURE_GRADE_OUTCOMES
  726. * @param string $feature FEATURE_xx constant for requested feature
  727. * @return mixed True if module supports feature, null if doesn't know
  728. */
  729. function choice_supports($feature) {
  730. switch($feature) {
  731. case FEATURE_GROUPS: return true;
  732. case FEATURE_GROUPINGS: return true;
  733. case FEATURE_MOD_INTRO: return true;
  734. case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
  735. case FEATURE_COMPLETION_HAS_RULES: return true;
  736. case FEATURE_GRADE_HAS_GRADE: return false;
  737. case FEATURE_GRADE_OUTCOMES: return false;
  738. case FEATURE_BACKUP_MOODLE2: return true;
  739. case FEATURE_SHOW_DESCRIPTION: return true;
  740. default: return null;
  741. }
  742. }
  743. /**
  744. * Adds module specific settings to the settings block
  745. *
  746. * @param settings_navigation $settings The settings navigation object
  747. * @param navigation_node $choicenode The node to add module settings to
  748. */
  749. function choice_extend_settings_navigation(settings_navigation $settings, navigation_node $choicenode) {
  750. global $PAGE;
  751. if (has_capability('mod/choice:readresponses', $PAGE->cm->context)) {
  752. $choicenode->add(get_string('responses', 'choice'),
  753. new moodle_url('/mod/choice/report.php', array('id' => $PAGE->cm->id)));
  754. }
  755. }
  756. /**
  757. * Return a list of page types
  758. * @param string $pagetype current page type
  759. * @param stdClass $parentcontext Block's parent context
  760. * @param stdClass $currentcontext Current context of block
  761. */
  762. function choice_page_type_list($pagetype, $parentcontext, $currentcontext) {
  763. $module_pagetype = array('mod-choice-*'=>get_string('page-mod-choice-x', 'choice'));
  764. return $module_pagetype;
  765. }
  766. /**
  767. * @deprecated since Moodle 3.3, when the block_course_overview block was removed.
  768. */
  769. function choice_print_overview() {
  770. throw new coding_exception('choice_print_overview() can not be used any more and is obsolete.');
  771. }
  772. /**
  773. * Get responses of a given user on a given choice.
  774. *
  775. * @param stdClass $choice Choice record
  776. * @param int $userid User id
  777. * @return array of choice answers records
  778. * @since Moodle 3.6
  779. */
  780. function choice_get_user_response($choice, $userid) {
  781. global $DB;
  782. return $DB->get_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $userid), 'optionid');
  783. }
  784. /**
  785. * Get my responses on a given choice.
  786. *
  787. * @param stdClass $choice Choice record
  788. * @return array of choice answers records
  789. * @since Moodle 3.0
  790. */
  791. function choice_get_my_response($choice) {
  792. global $USER;
  793. return choice_get_user_response($choice, $USER->id);
  794. }
  795. /**
  796. * Get all the responses on a given choice.
  797. *
  798. * @param stdClass $choice Choice record
  799. * @return array of choice answers records
  800. * @since Moodle 3.0
  801. */
  802. function choice_get_all_responses($choice) {
  803. global $DB;
  804. return $DB->get_records('choice_answers', array('choiceid' => $choice->id));
  805. }
  806. /**
  807. * Return true if we are allowd to view the choice results.
  808. *
  809. * @param stdClass $choice Choice record
  810. * @param rows|null $current my choice responses
  811. * @param bool|null $choiceopen if the choice is open
  812. * @return bool true if we can view the results, false otherwise.
  813. * @since Moodle 3.0
  814. */
  815. function choice_can_view_results($choice, $current = null, $choiceopen = null) {
  816. if (is_null($choiceopen)) {
  817. $timenow = time();
  818. if ($choice->timeopen != 0 && $timenow < $choice->timeopen) {
  819. // If the choice is not available, we can't see the results.
  820. return false;
  821. }
  822. if ($choice->timeclose != 0 && $timenow > $choice->timeclose) {
  823. $choiceopen = false;
  824. } else {
  825. $choiceopen = true;
  826. }
  827. }
  828. if (empty($current)) {
  829. $current = choice_get_my_response($choice);
  830. }
  831. if ($choice->showresults == CHOICE_SHOWRESULTS_ALWAYS or
  832. ($choice->showresults == CHOICE_SHOWRESULTS_AFTER_ANSWER and !empty($current)) or
  833. ($choice->showresults == CHOICE_SHOWRESULTS_AFTER_CLOSE and !$choiceopen)) {
  834. return true;
  835. }
  836. return false;
  837. }
  838. /**
  839. * Mark the activity completed (if required) and trigger the course_module_viewed event.
  840. *
  841. * @param stdClass $choice choice object
  842. * @param stdClass $course course object
  843. * @param stdClass $cm course module object
  844. * @param stdClass $context context object
  845. * @since Moodle 3.0
  846. */
  847. function choice_view($choice, $course, $cm, $context) {
  848. // Trigger course_module_viewed event.
  849. $params = array(
  850. 'context' => $context,
  851. 'objectid' => $choice->id
  852. );
  853. $event = \mod_choice\event\course_module_viewed::create($params);
  854. $event->add_record_snapshot('course_modules', $cm);
  855. $event->add_record_snapshot('course', $course);
  856. $event->add_record_snapshot('choice', $choice);
  857. $event->trigger();
  858. // Completion.
  859. $completion = new completion_info($course);
  860. $completion->set_module_viewed($cm);
  861. }
  862. /**
  863. * Check if a choice is available for the current user.
  864. *
  865. * @param stdClass $choice choice record
  866. * @return array status (available or not and possible warnings)
  867. */
  868. function choice_get_availability_status($choice) {
  869. $available = true;
  870. $warnings = array();
  871. $timenow = time();
  872. if (!empty($choice->timeopen) && ($choice->timeopen > $timenow)) {
  873. $available = false;
  874. $warnings['notopenyet'] = userdate($choice->timeopen);
  875. } else if (!empty($choice->timeclose) && ($timenow > $choice->timeclose)) {
  876. $available = false;
  877. $warnings['expired'] = userdate($choice->timeclose);
  878. }
  879. if (!$choice->allowupdate && choice_get_my_response($choice)) {
  880. $available = false;
  881. $warnings['choicesaved'] = '';
  882. }
  883. // Choice is available.
  884. return array($available, $warnings);
  885. }
  886. /**
  887. * This standard function will check all instances of this module
  888. * and make sure there are up-to-date events created for each of them.
  889. * If courseid = 0, then every choice event in the site is checked, else
  890. * only choice events belonging to the course specified are checked.
  891. * This function is used, in its new format, by restore_refresh_events()
  892. *
  893. * @param int $courseid
  894. * @param int|stdClass $instance Choice module instance or ID.
  895. * @param int|stdClass $cm Course module object or ID (not used in this module).
  896. * @return bool
  897. */
  898. function choice_refresh_events($courseid = 0, $instance = null, $cm = null) {
  899. global $DB, $CFG;
  900. require_once($CFG->dirroot.'/mod/choice/locallib.php');
  901. // If we have instance information then we can just update the one event instead of updating all events.
  902. if (isset($instance)) {
  903. if (!is_object($instance)) {
  904. $instance = $DB->get_record('choice', array('id' => $instance), '*', MUST_EXIST);
  905. }
  906. choice_set_events($instance);
  907. return true;
  908. }
  909. if ($courseid) {
  910. if (! $choices = $DB->get_records("choice", array("course" => $courseid))) {
  911. return true;
  912. }
  913. } else {
  914. if (! $choices = $DB->get_records("choice")) {
  915. return true;
  916. }
  917. }
  918. foreach ($choices as $choice) {
  919. choice_set_events($choice);
  920. }
  921. return true;
  922. }
  923. /**
  924. * Check if the module has any update that affects the current user since a given time.
  925. *
  926. * @param cm_info $cm course module data
  927. * @param int $from the time to check updates from
  928. * @param array $filter if we need to check only specific updates
  929. * @return stdClass an object with the different type of areas indicating if they were updated or not
  930. * @since Moodle 3.2
  931. */
  932. function choice_check_updates_since(cm_info $cm, $from, $filter = array()) {
  933. global $DB;
  934. $updates = new stdClass();
  935. $choice = $DB->get_record($cm->modname, array('id' => $cm->instance), '*', MUST_EXIST);
  936. list($available, $warnings) = choice_get_availability_status($choice);
  937. if (!$available) {
  938. return $updates;
  939. }
  940. $updates = course_check_module_updates_since($cm, $from, array(), $filter);
  941. if (!choice_can_view_results($choice)) {
  942. return $updates;
  943. }
  944. // Check if there are new responses in the choice.
  945. $updates->answers = (object) array('updated' => false);
  946. $select = 'choiceid = :id AND timemodified > :since';
  947. $params = array('id' => $choice->id, 'since' => $from);
  948. $answers = $DB->get_records_select('choice_answers', $select, $params, '', 'id');
  949. if (!empty($answers)) {
  950. $updates->answers->updated = true;
  951. $updates->answers->itemids = array_keys($answers);
  952. }
  953. return $updates;
  954. }
  955. /**
  956. * This function receives a calendar event and returns the action associated with it, or null if there is none.
  957. *
  958. * This is used by block_myoverview in order to display the event appropriately. If null is returned then the event
  959. * is not displayed on the block.
  960. *
  961. * @param calendar_event $event
  962. * @param \core_calendar\action_factory $factory
  963. * @param int $userid User id to use for all capability checks, etc. Set to 0 for current user (default).
  964. * @return \core_calendar\local\event\entities\action_interface|null
  965. */
  966. function mod_choice_core_calendar_provide_event_action(calendar_event $event,
  967. \core_calendar\action_factory $factory,
  968. int $userid = 0) {
  969. global $USER;
  970. if (!$userid) {
  971. $userid = $USER->id;
  972. }
  973. $cm = get_fast_modinfo($event->courseid, $userid)->instances['choice'][$event->instance];
  974. if (!$cm->uservisible) {
  975. // The module is not visible to the user for any reason.
  976. return null;
  977. }
  978. $completion = new \completion_info($cm->get_course());
  979. $completiondata = $completion->get_data($cm, false, $userid);
  980. if ($completiondata->completionstate != COMPLETION_INCOMPLETE) {
  981. return null;
  982. }
  983. $now = time();
  984. if (!empty($cm->customdata['timeclose']) && $cm->customdata['timeclose'] < $now) {
  985. // The choice has closed so the user can no longer submit anything.
  986. return null;
  987. }
  988. // The choice is actionable if we don't have a start time or the start time is
  989. // in the past.
  990. $actionable = (empty($cm->customdata['timeopen']) || $cm->customdata['timeopen'] <= $now);
  991. if ($actionable && choice_get_user_response((object)['id' => $event->instance], $userid)) {
  992. // There is no action if the user has already submitted their choice.
  993. return null;
  994. }
  995. return $factory->create_instance(
  996. get_string('viewchoices', 'choice'),
  997. new \moodle_url('/mod/choice/view.php', array('id' => $cm->id)),
  998. 1,
  999. $actionable
  1000. );
  1001. }
  1002. /**
  1003. * This function calculates the minimum and maximum cutoff values for the timestart of
  1004. * the given event.
  1005. *
  1006. * It will return an array with two values, the first being the minimum cutoff value and
  1007. * the second being the maximum cutoff value. Either or both values can be null, which
  1008. * indicates there is no minimum or maximum, respectively.
  1009. *
  1010. * If a cutoff is required then the function must return an array containing the cutoff
  1011. * timestamp and error string to display to the user if the cutoff value is violated.
  1012. *
  1013. * A minimum and maximum cutoff return value will look like:
  1014. * [
  1015. * [1505704373, 'The date must be after this date'],
  1016. * [1506741172, 'The date must be before this date']
  1017. * ]
  1018. *
  1019. * @param calendar_event $event The calendar event to get the time range for
  1020. * @param stdClass $choice The module instance to get the range from
  1021. */
  1022. function mod_choice_core_calendar_get_valid_event_timestart_range(\calendar_event $event, \stdClass $choice) {
  1023. $mindate = null;
  1024. $maxdate = null;
  1025. if ($event->eventtype == CHOICE_EVENT_TYPE_OPEN) {
  1026. if (!empty($choice->timeclose)) {
  1027. $maxdate = [
  1028. $choice->timeclose,
  1029. get_string('openafterclose', 'choice')
  1030. ];
  1031. }
  1032. } else if ($event->eventtype == CHOICE_EVENT_TYPE_CLOSE) {
  1033. if (!empty($choice->timeopen)) {
  1034. $mindate = [
  1035. $choice->timeopen,
  1036. get_string('closebeforeopen', 'choice')
  1037. ];
  1038. }
  1039. }
  1040. return [$mindate, $maxdate];
  1041. }
  1042. /**
  1043. * This function will update the choice module according to the
  1044. * event that has been modified.
  1045. *
  1046. * It will set the timeopen or timeclose value of the choice instance
  1047. * according to the type of event provided.
  1048. *
  1049. * @throws \moodle_exception
  1050. * @param \calendar_event $event
  1051. * @param stdClass $choice The module instance to get the range from
  1052. */
  1053. function mod_choice_core_calendar_event_timestart_updated(\calendar_event $event, \stdClass $choice) {
  1054. global $DB;
  1055. if (!in_array($event->eventtype, [CHOICE_EVENT_TYPE_OPEN, CHOICE_EVENT_TYPE_CLOSE])) {
  1056. return;
  1057. }
  1058. $courseid = $event->courseid;
  1059. $modulename = $event->modulename;
  1060. $instanceid = $event->instance;
  1061. $modified = false;
  1062. // Something weird going on. The event is for a different module so
  1063. // we should ignore it.
  1064. if ($modulename != 'choice') {
  1065. return;
  1066. }
  1067. if ($choice->id != $instanceid) {
  1068. return;
  1069. }
  1070. $coursemodule = get_fast_modinfo($courseid)->instances[$modulename][$instanceid];
  1071. $context = context_module::instance($coursemodule->id);
  1072. // The user does not have the capability to modify this activity.
  1073. if (!has_capability('moodle/course:manageactivities', $context)) {
  1074. return;
  1075. }
  1076. if ($event->eventtype == CHOICE_EVENT_TYPE_OPEN) {
  1077. // If the event is for the choice activity opening then we should
  1078. // set the start time of the choice activity to be the new start
  1079. // time of the event.
  1080. if ($choice->timeopen != $event->timestart) {
  1081. $choice->timeopen = $event->timestart;
  1082. $modified = true;
  1083. }
  1084. } else if ($event->eventtype == CHOICE_EVENT_TYPE_CLOSE) {
  1085. // If the event is for the choice activity closing then we should
  1086. // set the end time of the choice activity to be the new start
  1087. // time of the event.
  1088. if ($choice->timeclose != $event->timestart) {
  1089. $choice->timeclose = $event->timestart;
  1090. $modified = true;
  1091. }
  1092. }
  1093. if ($modified) {
  1094. $choice->timemodified = time();
  1095. // Persist the instance changes.
  1096. $DB->update_record('choice', $choice);
  1097. $event = \core\event\course_module_updated::create_from_cm($coursemodule, $context);
  1098. $event->trigger();
  1099. }
  1100. }
  1101. /**
  1102. * Get icon mapping for font-awesome.
  1103. */
  1104. function mod_choice_get_fontawesome_icon_map() {
  1105. return [
  1106. 'mod_choice:row' => 'fa-info',
  1107. 'mod_choice:column' => 'fa-columns',
  1108. ];
  1109. }
  1110. /**
  1111. * Add a get_coursemodule_info function in case any choice type wants to add 'extra' information
  1112. * for the course (see resource).
  1113. *
  1114. * Given a course_module object, this function returns any "extra" information that may be needed
  1115. * when printing this activity in a course listing. See get_array_of_activities() in course/lib.php.
  1116. *
  1117. * @param stdClass $coursemodule The coursemodule object (record).
  1118. * @return cached_cm_info An object on information that the courses
  1119. * will know about (most noticeably, an icon).
  1120. */
  1121. function choice_get_coursemodule_info($coursemodule) {
  1122. global $DB;
  1123. $dbparams = ['id' => $coursemodule->instance];
  1124. $fields = 'id, name, intro, introformat, completionsubmit, timeopen, timeclose';
  1125. if (!$choice = $DB->get_record('choice', $dbparams, $fields)) {
  1126. return false;
  1127. }
  1128. $result = new cached_cm_info();
  1129. $result->name = $choice->name;
  1130. if ($coursemodule->showdescription) {
  1131. // Convert intro to html. Do not filter cached version, filters run at display time.
  1132. $result->content = format_module_intro('choice', $choice, $coursemodule->id, false);
  1133. }
  1134. // Populate the custom completion rules as key => value pairs, but only if the completion mode is 'automatic'.
  1135. if ($coursemodule->completion == COMPLETION_TRACKING_AUTOMATIC) {
  1136. $result->customdata['customcompletionrules']['completionsubmit'] = $choice->completionsubmit;
  1137. }
  1138. // Populate some other values that can be used in calendar or on dashboard.
  1139. if ($choice->timeopen) {
  1140. $result->customdata['timeopen'] = $choice->timeopen;
  1141. }
  1142. if ($choice->timeclose) {
  1143. $result->customdata['timeclose'] = $choice->timeclose;
  1144. }
  1145. return $result;
  1146. }
  1147. /**
  1148. * Callback which returns human-readable strings describing the active completion custom rules for the module instance.
  1149. *
  1150. * @param cm_info|stdClass $cm object with fields ->completion and ->customdata['customcompletionrules']
  1151. * @return array $descriptions the array of descriptions for the custom rules.
  1152. */
  1153. function mod_choice_get_completion_active_rule_descriptions($cm) {
  1154. // Values will be present in cm_info, and we assume these are up to date.
  1155. if (empty($cm->customdata['customcompletionrules'])
  1156. || $cm->completion != COMPLETION_TRACKING_AUTOMATIC) {
  1157. return [];
  1158. }
  1159. $descriptions = [];
  1160. foreach ($cm->customdata['customcompletionrules'] as $key => $val) {
  1161. switch ($key) {
  1162. case 'completionsubmit':
  1163. if (!empty($val)) {
  1164. $descriptions[] = get_string('completionsubmit', 'choice');
  1165. }
  1166. break;
  1167. default:
  1168. break;
  1169. }
  1170. }
  1171. return $descriptions;
  1172. }
  1173. /**
  1174. * Callback to fetch the activity event type lang string.
  1175. *
  1176. * @param string $eventtype The event type.
  1177. * @return lang_string The event type lang string.
  1178. */
  1179. function mod_choice_core_calendar_get_event_action_string(string $eventtype): string {
  1180. $modulename = get_string('modulename', 'choice');
  1181. switch ($eventtype) {
  1182. case CHOICE_EVENT_TYPE_OPEN:
  1183. $identifier = 'calendarstart';
  1184. break;
  1185. case CHOICE_EVENT_TYPE_CLOSE:
  1186. $identifier = 'calendarend';
  1187. break;
  1188. default:
  1189. return get_string('requiresaction', 'calendar', $modulename);
  1190. }
  1191. return get_string($identifier, 'choice', $modulename);
  1192. }