PageRenderTime 54ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/mod/workshop/lib.php

https://bitbucket.org/moodle/moodle
PHP | 2284 lines | 1520 code | 275 blank | 489 comment | 328 complexity | 0e3146bccf3d04ae37bcb2e822dd7577 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0

Large files files are truncated, but you can click here to view the full file

  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. * Library of workshop module functions needed by Moodle core and other subsystems
  18. *
  19. * All the functions neeeded by Moodle core, gradebook, file subsystem etc
  20. * are placed here.
  21. *
  22. * @package mod_workshop
  23. * @copyright 2009 David Mudrak <david.mudrak@gmail.com>
  24. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25. */
  26. defined('MOODLE_INTERNAL') || die();
  27. require_once($CFG->dirroot . '/calendar/lib.php');
  28. define('WORKSHOP_EVENT_TYPE_SUBMISSION_OPEN', 'opensubmission');
  29. define('WORKSHOP_EVENT_TYPE_SUBMISSION_CLOSE', 'closesubmission');
  30. define('WORKSHOP_EVENT_TYPE_ASSESSMENT_OPEN', 'openassessment');
  31. define('WORKSHOP_EVENT_TYPE_ASSESSMENT_CLOSE', 'closeassessment');
  32. define('WORKSHOP_SUBMISSION_TYPE_DISABLED', 0);
  33. define('WORKSHOP_SUBMISSION_TYPE_AVAILABLE', 1);
  34. define('WORKSHOP_SUBMISSION_TYPE_REQUIRED', 2);
  35. ////////////////////////////////////////////////////////////////////////////////
  36. // Moodle core API //
  37. ////////////////////////////////////////////////////////////////////////////////
  38. /**
  39. * Returns the information if the module supports a feature
  40. *
  41. * @see plugin_supports() in lib/moodlelib.php
  42. * @param string $feature FEATURE_xx constant for requested feature
  43. * @return mixed true if the feature is supported, null if unknown
  44. */
  45. function workshop_supports($feature) {
  46. switch($feature) {
  47. case FEATURE_GRADE_HAS_GRADE: return true;
  48. case FEATURE_GROUPS: return true;
  49. case FEATURE_GROUPINGS: return true;
  50. case FEATURE_MOD_INTRO: return true;
  51. case FEATURE_BACKUP_MOODLE2: return true;
  52. case FEATURE_COMPLETION_TRACKS_VIEWS:
  53. return true;
  54. case FEATURE_SHOW_DESCRIPTION: return true;
  55. case FEATURE_PLAGIARISM: return true;
  56. default: return null;
  57. }
  58. }
  59. /**
  60. * Saves a new instance of the workshop into the database
  61. *
  62. * Given an object containing all the necessary data,
  63. * (defined by the form in mod_form.php) this function
  64. * will save a new instance and return the id number
  65. * of the new instance.
  66. *
  67. * @param stdClass $workshop An object from the form in mod_form.php
  68. * @return int The id of the newly inserted workshop record
  69. */
  70. function workshop_add_instance(stdclass $workshop) {
  71. global $CFG, $DB;
  72. require_once(__DIR__ . '/locallib.php');
  73. $workshop->phase = workshop::PHASE_SETUP;
  74. $workshop->timecreated = time();
  75. $workshop->timemodified = $workshop->timecreated;
  76. $workshop->useexamples = (int)!empty($workshop->useexamples);
  77. $workshop->usepeerassessment = 1;
  78. $workshop->useselfassessment = (int)!empty($workshop->useselfassessment);
  79. $workshop->latesubmissions = (int)!empty($workshop->latesubmissions);
  80. $workshop->phaseswitchassessment = (int)!empty($workshop->phaseswitchassessment);
  81. $workshop->evaluation = 'best';
  82. if (isset($workshop->gradinggradepass)) {
  83. $workshop->gradinggradepass = (float)unformat_float($workshop->gradinggradepass);
  84. }
  85. if (isset($workshop->submissiongradepass)) {
  86. $workshop->submissiongradepass = (float)unformat_float($workshop->submissiongradepass);
  87. }
  88. if (isset($workshop->submissionfiletypes)) {
  89. $filetypesutil = new \core_form\filetypes_util();
  90. $submissionfiletypes = $filetypesutil->normalize_file_types($workshop->submissionfiletypes);
  91. $workshop->submissionfiletypes = implode(' ', $submissionfiletypes);
  92. }
  93. if (isset($workshop->overallfeedbackfiletypes)) {
  94. $filetypesutil = new \core_form\filetypes_util();
  95. $overallfeedbackfiletypes = $filetypesutil->normalize_file_types($workshop->overallfeedbackfiletypes);
  96. $workshop->overallfeedbackfiletypes = implode(' ', $overallfeedbackfiletypes);
  97. }
  98. // insert the new record so we get the id
  99. $workshop->id = $DB->insert_record('workshop', $workshop);
  100. // we need to use context now, so we need to make sure all needed info is already in db
  101. $cmid = $workshop->coursemodule;
  102. $DB->set_field('course_modules', 'instance', $workshop->id, array('id' => $cmid));
  103. $context = context_module::instance($cmid);
  104. // process the custom wysiwyg editors
  105. if ($draftitemid = $workshop->instructauthorseditor['itemid']) {
  106. $workshop->instructauthors = file_save_draft_area_files($draftitemid, $context->id, 'mod_workshop', 'instructauthors',
  107. 0, workshop::instruction_editors_options($context), $workshop->instructauthorseditor['text']);
  108. $workshop->instructauthorsformat = $workshop->instructauthorseditor['format'];
  109. }
  110. if ($draftitemid = $workshop->instructreviewerseditor['itemid']) {
  111. $workshop->instructreviewers = file_save_draft_area_files($draftitemid, $context->id, 'mod_workshop', 'instructreviewers',
  112. 0, workshop::instruction_editors_options($context), $workshop->instructreviewerseditor['text']);
  113. $workshop->instructreviewersformat = $workshop->instructreviewerseditor['format'];
  114. }
  115. if ($draftitemid = $workshop->conclusioneditor['itemid']) {
  116. $workshop->conclusion = file_save_draft_area_files($draftitemid, $context->id, 'mod_workshop', 'conclusion',
  117. 0, workshop::instruction_editors_options($context), $workshop->conclusioneditor['text']);
  118. $workshop->conclusionformat = $workshop->conclusioneditor['format'];
  119. }
  120. // re-save the record with the replaced URLs in editor fields
  121. $DB->update_record('workshop', $workshop);
  122. // create gradebook items
  123. workshop_grade_item_update($workshop);
  124. workshop_grade_item_category_update($workshop);
  125. // create calendar events
  126. workshop_calendar_update($workshop, $workshop->coursemodule);
  127. if (!empty($workshop->completionexpected)) {
  128. \core_completion\api::update_completion_date_event($cmid, 'workshop', $workshop->id, $workshop->completionexpected);
  129. }
  130. return $workshop->id;
  131. }
  132. /**
  133. * Given an object containing all the necessary data,
  134. * (defined by the form in mod_form.php) this function
  135. * will update an existing instance with new data.
  136. *
  137. * @param stdClass $workshop An object from the form in mod_form.php
  138. * @return bool success
  139. */
  140. function workshop_update_instance(stdclass $workshop) {
  141. global $CFG, $DB;
  142. require_once(__DIR__ . '/locallib.php');
  143. $workshop->timemodified = time();
  144. $workshop->id = $workshop->instance;
  145. $workshop->useexamples = (int)!empty($workshop->useexamples);
  146. $workshop->usepeerassessment = 1;
  147. $workshop->useselfassessment = (int)!empty($workshop->useselfassessment);
  148. $workshop->latesubmissions = (int)!empty($workshop->latesubmissions);
  149. $workshop->phaseswitchassessment = (int)!empty($workshop->phaseswitchassessment);
  150. if (isset($workshop->gradinggradepass)) {
  151. $workshop->gradinggradepass = (float)unformat_float($workshop->gradinggradepass);
  152. }
  153. if (isset($workshop->submissiongradepass)) {
  154. $workshop->submissiongradepass = (float)unformat_float($workshop->submissiongradepass);
  155. }
  156. if (isset($workshop->submissionfiletypes)) {
  157. $filetypesutil = new \core_form\filetypes_util();
  158. $submissionfiletypes = $filetypesutil->normalize_file_types($workshop->submissionfiletypes);
  159. $workshop->submissionfiletypes = implode(' ', $submissionfiletypes);
  160. }
  161. if (isset($workshop->overallfeedbackfiletypes)) {
  162. $filetypesutil = new \core_form\filetypes_util();
  163. $overallfeedbackfiletypes = $filetypesutil->normalize_file_types($workshop->overallfeedbackfiletypes);
  164. $workshop->overallfeedbackfiletypes = implode(' ', $overallfeedbackfiletypes);
  165. }
  166. // todo - if the grading strategy is being changed, we may want to replace all aggregated peer grades with nulls
  167. $DB->update_record('workshop', $workshop);
  168. $context = context_module::instance($workshop->coursemodule);
  169. // process the custom wysiwyg editors
  170. if ($draftitemid = $workshop->instructauthorseditor['itemid']) {
  171. $workshop->instructauthors = file_save_draft_area_files($draftitemid, $context->id, 'mod_workshop', 'instructauthors',
  172. 0, workshop::instruction_editors_options($context), $workshop->instructauthorseditor['text']);
  173. $workshop->instructauthorsformat = $workshop->instructauthorseditor['format'];
  174. }
  175. if ($draftitemid = $workshop->instructreviewerseditor['itemid']) {
  176. $workshop->instructreviewers = file_save_draft_area_files($draftitemid, $context->id, 'mod_workshop', 'instructreviewers',
  177. 0, workshop::instruction_editors_options($context), $workshop->instructreviewerseditor['text']);
  178. $workshop->instructreviewersformat = $workshop->instructreviewerseditor['format'];
  179. }
  180. if ($draftitemid = $workshop->conclusioneditor['itemid']) {
  181. $workshop->conclusion = file_save_draft_area_files($draftitemid, $context->id, 'mod_workshop', 'conclusion',
  182. 0, workshop::instruction_editors_options($context), $workshop->conclusioneditor['text']);
  183. $workshop->conclusionformat = $workshop->conclusioneditor['format'];
  184. }
  185. // re-save the record with the replaced URLs in editor fields
  186. $DB->update_record('workshop', $workshop);
  187. // update gradebook items
  188. workshop_grade_item_update($workshop);
  189. workshop_grade_item_category_update($workshop);
  190. // update calendar events
  191. workshop_calendar_update($workshop, $workshop->coursemodule);
  192. $completionexpected = (!empty($workshop->completionexpected)) ? $workshop->completionexpected : null;
  193. \core_completion\api::update_completion_date_event($workshop->coursemodule, 'workshop', $workshop->id, $completionexpected);
  194. return true;
  195. }
  196. /**
  197. * Given an ID of an instance of this module,
  198. * this function will permanently delete the instance
  199. * and any data that depends on it.
  200. *
  201. * @param int $id Id of the module instance
  202. * @return boolean Success/Failure
  203. */
  204. function workshop_delete_instance($id) {
  205. global $CFG, $DB;
  206. require_once($CFG->libdir.'/gradelib.php');
  207. if (! $workshop = $DB->get_record('workshop', array('id' => $id))) {
  208. return false;
  209. }
  210. // delete all associated aggregations
  211. $DB->delete_records('workshop_aggregations', array('workshopid' => $workshop->id));
  212. // get the list of ids of all submissions
  213. $submissions = $DB->get_records('workshop_submissions', array('workshopid' => $workshop->id), '', 'id');
  214. // get the list of all allocated assessments
  215. $assessments = $DB->get_records_list('workshop_assessments', 'submissionid', array_keys($submissions), '', 'id');
  216. // delete the associated records from the workshop core tables
  217. $DB->delete_records_list('workshop_grades', 'assessmentid', array_keys($assessments));
  218. $DB->delete_records_list('workshop_assessments', 'id', array_keys($assessments));
  219. $DB->delete_records_list('workshop_submissions', 'id', array_keys($submissions));
  220. // call the static clean-up methods of all available subplugins
  221. $strategies = core_component::get_plugin_list('workshopform');
  222. foreach ($strategies as $strategy => $path) {
  223. require_once($path.'/lib.php');
  224. $classname = 'workshop_'.$strategy.'_strategy';
  225. call_user_func($classname.'::delete_instance', $workshop->id);
  226. }
  227. $allocators = core_component::get_plugin_list('workshopallocation');
  228. foreach ($allocators as $allocator => $path) {
  229. require_once($path.'/lib.php');
  230. $classname = 'workshop_'.$allocator.'_allocator';
  231. call_user_func($classname.'::delete_instance', $workshop->id);
  232. }
  233. $evaluators = core_component::get_plugin_list('workshopeval');
  234. foreach ($evaluators as $evaluator => $path) {
  235. require_once($path.'/lib.php');
  236. $classname = 'workshop_'.$evaluator.'_evaluation';
  237. call_user_func($classname.'::delete_instance', $workshop->id);
  238. }
  239. // delete the calendar events
  240. $events = $DB->get_records('event', array('modulename' => 'workshop', 'instance' => $workshop->id));
  241. foreach ($events as $event) {
  242. $event = calendar_event::load($event);
  243. $event->delete();
  244. }
  245. // gradebook cleanup
  246. grade_update('mod/workshop', $workshop->course, 'mod', 'workshop', $workshop->id, 0, null, array('deleted' => true));
  247. grade_update('mod/workshop', $workshop->course, 'mod', 'workshop', $workshop->id, 1, null, array('deleted' => true));
  248. // finally remove the workshop record itself
  249. // We must delete the module record after we delete the grade item.
  250. $DB->delete_records('workshop', array('id' => $workshop->id));
  251. return true;
  252. }
  253. /**
  254. * This standard function will check all instances of this module
  255. * and make sure there are up-to-date events created for each of them.
  256. * If courseid = 0, then every workshop event in the site is checked, else
  257. * only workshop events belonging to the course specified are checked.
  258. *
  259. * @param integer $courseid The Course ID.
  260. * @param int|stdClass $instance workshop module instance or ID.
  261. * @param int|stdClass $cm Course module object or ID.
  262. * @return bool Returns true if the calendar events were successfully updated.
  263. */
  264. function workshop_refresh_events($courseid = 0, $instance = null, $cm = null) {
  265. global $DB;
  266. // If we have instance information then we can just update the one event instead of updating all events.
  267. if (isset($instance)) {
  268. if (!is_object($instance)) {
  269. $instance = $DB->get_record('workshop', array('id' => $instance), '*', MUST_EXIST);
  270. }
  271. if (isset($cm)) {
  272. if (!is_object($cm)) {
  273. $cm = (object)array('id' => $cm);
  274. }
  275. } else {
  276. $cm = get_coursemodule_from_instance('workshop', $instance->id);
  277. }
  278. workshop_calendar_update($instance, $cm->id);
  279. return true;
  280. }
  281. if ($courseid) {
  282. // Make sure that the course id is numeric.
  283. if (!is_numeric($courseid)) {
  284. return false;
  285. }
  286. if (!$workshops = $DB->get_records('workshop', array('course' => $courseid))) {
  287. return false;
  288. }
  289. } else {
  290. if (!$workshops = $DB->get_records('workshop')) {
  291. return false;
  292. }
  293. }
  294. foreach ($workshops as $workshop) {
  295. if (!$cm = get_coursemodule_from_instance('workshop', $workshop->id, $courseid, false)) {
  296. continue;
  297. }
  298. workshop_calendar_update($workshop, $cm->id);
  299. }
  300. return true;
  301. }
  302. /**
  303. * List the actions that correspond to a view of this module.
  304. * This is used by the participation report.
  305. *
  306. * Note: This is not used by new logging system. Event with
  307. * crud = 'r' and edulevel = LEVEL_PARTICIPATING will
  308. * be considered as view action.
  309. *
  310. * @return array
  311. */
  312. function workshop_get_view_actions() {
  313. return array('view', 'view all', 'view submission', 'view example');
  314. }
  315. /**
  316. * List the actions that correspond to a post of this module.
  317. * This is used by the participation report.
  318. *
  319. * Note: This is not used by new logging system. Event with
  320. * crud = ('c' || 'u' || 'd') and edulevel = LEVEL_PARTICIPATING
  321. * will be considered as post action.
  322. *
  323. * @return array
  324. */
  325. function workshop_get_post_actions() {
  326. return array('add', 'add assessment', 'add example', 'add submission',
  327. 'update', 'update assessment', 'update example', 'update submission');
  328. }
  329. /**
  330. * Return a small object with summary information about what a
  331. * user has done with a given particular instance of this module
  332. * Used for user activity reports.
  333. * $return->time = the time they did it
  334. * $return->info = a short text description
  335. *
  336. * @param stdClass $course The course record.
  337. * @param stdClass $user The user record.
  338. * @param cm_info|stdClass $mod The course module info object or record.
  339. * @param stdClass $workshop The workshop instance record.
  340. * @return stdclass|null
  341. */
  342. function workshop_user_outline($course, $user, $mod, $workshop) {
  343. global $CFG, $DB;
  344. require_once($CFG->libdir.'/gradelib.php');
  345. $grades = grade_get_grades($course->id, 'mod', 'workshop', $workshop->id, $user->id);
  346. $submissiongrade = null;
  347. $assessmentgrade = null;
  348. $info = '';
  349. $time = 0;
  350. if (!empty($grades->items[0]->grades)) {
  351. $submissiongrade = reset($grades->items[0]->grades);
  352. $time = max($time, $submissiongrade->dategraded);
  353. if (!$submissiongrade->hidden || has_capability('moodle/grade:viewhidden', context_course::instance($course->id))) {
  354. $info .= get_string('submissiongrade', 'workshop') . ': ' . $submissiongrade->str_long_grade
  355. . html_writer::empty_tag('br');
  356. } else {
  357. $info .= get_string('submissiongrade', 'workshop') . ': ' . get_string('hidden', 'grades')
  358. . html_writer::empty_tag('br');
  359. }
  360. }
  361. if (!empty($grades->items[1]->grades)) {
  362. $assessmentgrade = reset($grades->items[1]->grades);
  363. $time = max($time, $assessmentgrade->dategraded);
  364. if (!$assessmentgrade->hidden || has_capability('moodle/grade:viewhidden', context_course::instance($course->id))) {
  365. $info .= get_string('gradinggrade', 'workshop') . ': ' . $assessmentgrade->str_long_grade;
  366. } else {
  367. $info .= get_string('gradinggrade', 'workshop') . ': ' . get_string('hidden', 'grades');
  368. }
  369. }
  370. if (!empty($info) and !empty($time)) {
  371. $return = new stdclass();
  372. $return->time = $time;
  373. $return->info = $info;
  374. return $return;
  375. }
  376. return null;
  377. }
  378. /**
  379. * Print a detailed representation of what a user has done with
  380. * a given particular instance of this module, for user activity reports.
  381. *
  382. * @param stdClass $course The course record.
  383. * @param stdClass $user The user record.
  384. * @param cm_info|stdClass $mod The course module info object or record.
  385. * @param stdClass $workshop The workshop instance record.
  386. * @return string HTML
  387. */
  388. function workshop_user_complete($course, $user, $mod, $workshop) {
  389. global $CFG, $DB, $OUTPUT;
  390. require_once(__DIR__.'/locallib.php');
  391. require_once($CFG->libdir.'/gradelib.php');
  392. $workshop = new workshop($workshop, $mod, $course);
  393. $grades = grade_get_grades($course->id, 'mod', 'workshop', $workshop->id, $user->id);
  394. if (!empty($grades->items[0]->grades)) {
  395. $submissiongrade = reset($grades->items[0]->grades);
  396. if (!$submissiongrade->hidden || has_capability('moodle/grade:viewhidden', context_course::instance($course->id))) {
  397. $info = get_string('submissiongrade', 'workshop') . ': ' . $submissiongrade->str_long_grade;
  398. } else {
  399. $info = get_string('submissiongrade', 'workshop') . ': ' . get_string('hidden', 'grades');
  400. }
  401. echo html_writer::tag('li', $info, array('class'=>'submissiongrade'));
  402. }
  403. if (!empty($grades->items[1]->grades)) {
  404. $assessmentgrade = reset($grades->items[1]->grades);
  405. if (!$assessmentgrade->hidden || has_capability('moodle/grade:viewhidden', context_course::instance($course->id))) {
  406. $info = get_string('gradinggrade', 'workshop') . ': ' . $assessmentgrade->str_long_grade;
  407. } else {
  408. $info = get_string('gradinggrade', 'workshop') . ': ' . get_string('hidden', 'grades');
  409. }
  410. echo html_writer::tag('li', $info, array('class'=>'gradinggrade'));
  411. }
  412. if (has_capability('mod/workshop:viewallsubmissions', $workshop->context)) {
  413. $canviewsubmission = true;
  414. if (groups_get_activity_groupmode($workshop->cm) == SEPARATEGROUPS) {
  415. // user must have accessallgroups or share at least one group with the submission author
  416. if (!has_capability('moodle/site:accessallgroups', $workshop->context)) {
  417. $usersgroups = groups_get_activity_allowed_groups($workshop->cm);
  418. $authorsgroups = groups_get_all_groups($workshop->course->id, $user->id, $workshop->cm->groupingid, 'g.id');
  419. $sharedgroups = array_intersect_key($usersgroups, $authorsgroups);
  420. if (empty($sharedgroups)) {
  421. $canviewsubmission = false;
  422. }
  423. }
  424. }
  425. if ($canviewsubmission and $submission = $workshop->get_submission_by_author($user->id)) {
  426. $title = format_string($submission->title);
  427. $url = $workshop->submission_url($submission->id);
  428. $link = html_writer::link($url, $title);
  429. $info = get_string('submission', 'workshop').': '.$link;
  430. echo html_writer::tag('li', $info, array('class'=>'submission'));
  431. }
  432. }
  433. if (has_capability('mod/workshop:viewallassessments', $workshop->context)) {
  434. if ($assessments = $workshop->get_assessments_by_reviewer($user->id)) {
  435. foreach ($assessments as $assessment) {
  436. $a = new stdclass();
  437. $a->submissionurl = $workshop->submission_url($assessment->submissionid)->out();
  438. $a->assessmenturl = $workshop->assess_url($assessment->id)->out();
  439. $a->submissiontitle = s($assessment->submissiontitle);
  440. echo html_writer::tag('li', get_string('assessmentofsubmission', 'workshop', $a));
  441. }
  442. }
  443. }
  444. }
  445. /**
  446. * Given a course and a time, this module should find recent activity
  447. * that has occurred in workshop activities and print it out.
  448. * Return true if there was output, or false is there was none.
  449. *
  450. * @param stdClass $course
  451. * @param bool $viewfullnames
  452. * @param int $timestart
  453. * @return boolean
  454. */
  455. function workshop_print_recent_activity($course, $viewfullnames, $timestart) {
  456. global $CFG, $USER, $DB, $OUTPUT;
  457. $userfieldsapi = \core_user\fields::for_name();
  458. $authoramefields = $userfieldsapi->get_sql('author', false, 'author', '', false)->selects;
  459. $reviewerfields = $userfieldsapi->get_sql('reviewer', false, 'reviewer', '', false)->selects;
  460. $sql = "SELECT s.id AS submissionid, s.title AS submissiontitle, s.timemodified AS submissionmodified,
  461. author.id AS authorid, $authoramefields, a.id AS assessmentid, a.timemodified AS assessmentmodified,
  462. reviewer.id AS reviewerid, $reviewerfields, cm.id AS cmid
  463. FROM {workshop} w
  464. INNER JOIN {course_modules} cm ON cm.instance = w.id
  465. INNER JOIN {modules} md ON md.id = cm.module
  466. INNER JOIN {workshop_submissions} s ON s.workshopid = w.id
  467. INNER JOIN {user} author ON s.authorid = author.id
  468. LEFT JOIN {workshop_assessments} a ON a.submissionid = s.id
  469. LEFT JOIN {user} reviewer ON a.reviewerid = reviewer.id
  470. WHERE cm.course = ?
  471. AND md.name = 'workshop'
  472. AND s.example = 0
  473. AND (s.timemodified > ? OR a.timemodified > ?)
  474. ORDER BY s.timemodified";
  475. $rs = $DB->get_recordset_sql($sql, array($course->id, $timestart, $timestart));
  476. $modinfo = get_fast_modinfo($course); // reference needed because we might load the groups
  477. $submissions = array(); // recent submissions indexed by submission id
  478. $assessments = array(); // recent assessments indexed by assessment id
  479. $users = array();
  480. foreach ($rs as $activity) {
  481. if (!array_key_exists($activity->cmid, $modinfo->cms)) {
  482. // this should not happen but just in case
  483. continue;
  484. }
  485. $cm = $modinfo->cms[$activity->cmid];
  486. if (!$cm->uservisible) {
  487. continue;
  488. }
  489. // remember all user names we can use later
  490. if (empty($users[$activity->authorid])) {
  491. $u = new stdclass();
  492. $users[$activity->authorid] = username_load_fields_from_object($u, $activity, 'author');
  493. }
  494. if ($activity->reviewerid and empty($users[$activity->reviewerid])) {
  495. $u = new stdclass();
  496. $users[$activity->reviewerid] = username_load_fields_from_object($u, $activity, 'reviewer');
  497. }
  498. $context = context_module::instance($cm->id);
  499. $groupmode = groups_get_activity_groupmode($cm, $course);
  500. if ($activity->submissionmodified > $timestart and empty($submissions[$activity->submissionid])) {
  501. $s = new stdclass();
  502. $s->title = $activity->submissiontitle;
  503. $s->authorid = $activity->authorid;
  504. $s->timemodified = $activity->submissionmodified;
  505. $s->cmid = $activity->cmid;
  506. if ($activity->authorid == $USER->id || has_capability('mod/workshop:viewauthornames', $context)) {
  507. $s->authornamevisible = true;
  508. } else {
  509. $s->authornamevisible = false;
  510. }
  511. // the following do-while wrapper allows to break from deeply nested if-statements
  512. do {
  513. if ($s->authorid === $USER->id) {
  514. // own submissions always visible
  515. $submissions[$activity->submissionid] = $s;
  516. break;
  517. }
  518. if (has_capability('mod/workshop:viewallsubmissions', $context)) {
  519. if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
  520. if (isguestuser()) {
  521. // shortcut - guest user does not belong into any group
  522. break;
  523. }
  524. // this might be slow - show only submissions by users who share group with me in this cm
  525. if (!$modinfo->get_groups($cm->groupingid)) {
  526. break;
  527. }
  528. $authorsgroups = groups_get_all_groups($course->id, $s->authorid, $cm->groupingid);
  529. if (is_array($authorsgroups)) {
  530. $authorsgroups = array_keys($authorsgroups);
  531. $intersect = array_intersect($authorsgroups, $modinfo->get_groups($cm->groupingid));
  532. if (empty($intersect)) {
  533. break;
  534. } else {
  535. // can see all submissions and shares a group with the author
  536. $submissions[$activity->submissionid] = $s;
  537. break;
  538. }
  539. }
  540. } else {
  541. // can see all submissions from all groups
  542. $submissions[$activity->submissionid] = $s;
  543. }
  544. }
  545. } while (0);
  546. }
  547. if ($activity->assessmentmodified > $timestart and empty($assessments[$activity->assessmentid])) {
  548. $a = new stdclass();
  549. $a->submissionid = $activity->submissionid;
  550. $a->submissiontitle = $activity->submissiontitle;
  551. $a->reviewerid = $activity->reviewerid;
  552. $a->timemodified = $activity->assessmentmodified;
  553. $a->cmid = $activity->cmid;
  554. if ($activity->reviewerid == $USER->id || has_capability('mod/workshop:viewreviewernames', $context)) {
  555. $a->reviewernamevisible = true;
  556. } else {
  557. $a->reviewernamevisible = false;
  558. }
  559. // the following do-while wrapper allows to break from deeply nested if-statements
  560. do {
  561. if ($a->reviewerid === $USER->id) {
  562. // own assessments always visible
  563. $assessments[$activity->assessmentid] = $a;
  564. break;
  565. }
  566. if (has_capability('mod/workshop:viewallassessments', $context)) {
  567. if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
  568. if (isguestuser()) {
  569. // shortcut - guest user does not belong into any group
  570. break;
  571. }
  572. // this might be slow - show only submissions by users who share group with me in this cm
  573. if (!$modinfo->get_groups($cm->groupingid)) {
  574. break;
  575. }
  576. $reviewersgroups = groups_get_all_groups($course->id, $a->reviewerid, $cm->groupingid);
  577. if (is_array($reviewersgroups)) {
  578. $reviewersgroups = array_keys($reviewersgroups);
  579. $intersect = array_intersect($reviewersgroups, $modinfo->get_groups($cm->groupingid));
  580. if (empty($intersect)) {
  581. break;
  582. } else {
  583. // can see all assessments and shares a group with the reviewer
  584. $assessments[$activity->assessmentid] = $a;
  585. break;
  586. }
  587. }
  588. } else {
  589. // can see all assessments from all groups
  590. $assessments[$activity->assessmentid] = $a;
  591. }
  592. }
  593. } while (0);
  594. }
  595. }
  596. $rs->close();
  597. $shown = false;
  598. if (!empty($submissions)) {
  599. $shown = true;
  600. echo $OUTPUT->heading(get_string('recentsubmissions', 'workshop') . ':', 6);
  601. foreach ($submissions as $id => $submission) {
  602. $link = new moodle_url('/mod/workshop/submission.php', array('id'=>$id, 'cmid'=>$submission->cmid));
  603. if ($submission->authornamevisible) {
  604. $author = $users[$submission->authorid];
  605. } else {
  606. $author = null;
  607. }
  608. print_recent_activity_note($submission->timemodified, $author, $submission->title, $link->out(), false, $viewfullnames);
  609. }
  610. }
  611. if (!empty($assessments)) {
  612. $shown = true;
  613. echo $OUTPUT->heading(get_string('recentassessments', 'workshop') . ':', 6);
  614. core_collator::asort_objects_by_property($assessments, 'timemodified');
  615. foreach ($assessments as $id => $assessment) {
  616. $link = new moodle_url('/mod/workshop/assessment.php', array('asid' => $id));
  617. if ($assessment->reviewernamevisible) {
  618. $reviewer = $users[$assessment->reviewerid];
  619. } else {
  620. $reviewer = null;
  621. }
  622. print_recent_activity_note($assessment->timemodified, $reviewer, $assessment->submissiontitle, $link->out(), false, $viewfullnames);
  623. }
  624. }
  625. if ($shown) {
  626. return true;
  627. }
  628. return false;
  629. }
  630. /**
  631. * Returns all activity in course workshops since a given time
  632. *
  633. * @param array $activities sequentially indexed array of objects
  634. * @param int $index
  635. * @param int $timestart
  636. * @param int $courseid
  637. * @param int $cmid
  638. * @param int $userid defaults to 0
  639. * @param int $groupid defaults to 0
  640. * @return void adds items into $activities and increases $index
  641. */
  642. function workshop_get_recent_mod_activity(&$activities, &$index, $timestart, $courseid, $cmid, $userid=0, $groupid=0) {
  643. global $CFG, $COURSE, $USER, $DB;
  644. if ($COURSE->id == $courseid) {
  645. $course = $COURSE;
  646. } else {
  647. $course = $DB->get_record('course', array('id'=>$courseid));
  648. }
  649. $modinfo = get_fast_modinfo($course);
  650. $cm = $modinfo->cms[$cmid];
  651. $params = array();
  652. if ($userid) {
  653. $userselect = "AND (author.id = :authorid OR reviewer.id = :reviewerid)";
  654. $params['authorid'] = $userid;
  655. $params['reviewerid'] = $userid;
  656. } else {
  657. $userselect = "";
  658. }
  659. if ($groupid) {
  660. $groupselect = "AND (authorgroupmembership.groupid = :authorgroupid OR reviewergroupmembership.groupid = :reviewergroupid)";
  661. $groupjoin = "LEFT JOIN {groups_members} authorgroupmembership ON authorgroupmembership.userid = author.id
  662. LEFT JOIN {groups_members} reviewergroupmembership ON reviewergroupmembership.userid = reviewer.id";
  663. $params['authorgroupid'] = $groupid;
  664. $params['reviewergroupid'] = $groupid;
  665. } else {
  666. $groupselect = "";
  667. $groupjoin = "";
  668. }
  669. $params['cminstance'] = $cm->instance;
  670. $params['submissionmodified'] = $timestart;
  671. $params['assessmentmodified'] = $timestart;
  672. $userfieldsapi = \core_user\fields::for_name();
  673. $authornamefields = $userfieldsapi->get_sql('author', false, 'author', '', false)->selects;
  674. $reviewerfields = $userfieldsapi->get_sql('reviewer', false, 'reviewer', '', false)->selects;
  675. $sql = "SELECT s.id AS submissionid, s.title AS submissiontitle, s.timemodified AS submissionmodified,
  676. author.id AS authorid, $authornamefields, author.picture AS authorpicture, author.imagealt AS authorimagealt,
  677. author.email AS authoremail, a.id AS assessmentid, a.timemodified AS assessmentmodified,
  678. reviewer.id AS reviewerid, $reviewerfields, reviewer.picture AS reviewerpicture,
  679. reviewer.imagealt AS reviewerimagealt, reviewer.email AS revieweremail
  680. FROM {workshop_submissions} s
  681. INNER JOIN {workshop} w ON s.workshopid = w.id
  682. INNER JOIN {user} author ON s.authorid = author.id
  683. LEFT JOIN {workshop_assessments} a ON a.submissionid = s.id
  684. LEFT JOIN {user} reviewer ON a.reviewerid = reviewer.id
  685. $groupjoin
  686. WHERE w.id = :cminstance
  687. AND s.example = 0
  688. $userselect $groupselect
  689. AND (s.timemodified > :submissionmodified OR a.timemodified > :assessmentmodified)
  690. ORDER BY s.timemodified ASC, a.timemodified ASC";
  691. $rs = $DB->get_recordset_sql($sql, $params);
  692. $groupmode = groups_get_activity_groupmode($cm, $course);
  693. $context = context_module::instance($cm->id);
  694. $grader = has_capability('moodle/grade:viewall', $context);
  695. $accessallgroups = has_capability('moodle/site:accessallgroups', $context);
  696. $viewauthors = has_capability('mod/workshop:viewauthornames', $context);
  697. $viewreviewers = has_capability('mod/workshop:viewreviewernames', $context);
  698. $submissions = array(); // recent submissions indexed by submission id
  699. $assessments = array(); // recent assessments indexed by assessment id
  700. $users = array();
  701. foreach ($rs as $activity) {
  702. // remember all user names we can use later
  703. if (empty($users[$activity->authorid])) {
  704. $u = new stdclass();
  705. $additionalfields = explode(',', implode(',', \core_user\fields::get_picture_fields()));
  706. $u = username_load_fields_from_object($u, $activity, 'author', $additionalfields);
  707. $users[$activity->authorid] = $u;
  708. }
  709. if ($activity->reviewerid and empty($users[$activity->reviewerid])) {
  710. $u = new stdclass();
  711. $additionalfields = explode(',', implode(',', \core_user\fields::get_picture_fields()));
  712. $u = username_load_fields_from_object($u, $activity, 'reviewer', $additionalfields);
  713. $users[$activity->reviewerid] = $u;
  714. }
  715. if ($activity->submissionmodified > $timestart and empty($submissions[$activity->submissionid])) {
  716. $s = new stdclass();
  717. $s->id = $activity->submissionid;
  718. $s->title = $activity->submissiontitle;
  719. $s->authorid = $activity->authorid;
  720. $s->timemodified = $activity->submissionmodified;
  721. if ($activity->authorid == $USER->id || has_capability('mod/workshop:viewauthornames', $context)) {
  722. $s->authornamevisible = true;
  723. } else {
  724. $s->authornamevisible = false;
  725. }
  726. // the following do-while wrapper allows to break from deeply nested if-statements
  727. do {
  728. if ($s->authorid === $USER->id) {
  729. // own submissions always visible
  730. $submissions[$activity->submissionid] = $s;
  731. break;
  732. }
  733. if (has_capability('mod/workshop:viewallsubmissions', $context)) {
  734. if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
  735. if (isguestuser()) {
  736. // shortcut - guest user does not belong into any group
  737. break;
  738. }
  739. // this might be slow - show only submissions by users who share group with me in this cm
  740. if (!$modinfo->get_groups($cm->groupingid)) {
  741. break;
  742. }
  743. $authorsgroups = groups_get_all_groups($course->id, $s->authorid, $cm->groupingid);
  744. if (is_array($authorsgroups)) {
  745. $authorsgroups = array_keys($authorsgroups);
  746. $intersect = array_intersect($authorsgroups, $modinfo->get_groups($cm->groupingid));
  747. if (empty($intersect)) {
  748. break;
  749. } else {
  750. // can see all submissions and shares a group with the author
  751. $submissions[$activity->submissionid] = $s;
  752. break;
  753. }
  754. }
  755. } else {
  756. // can see all submissions from all groups
  757. $submissions[$activity->submissionid] = $s;
  758. }
  759. }
  760. } while (0);
  761. }
  762. if ($activity->assessmentmodified > $timestart and empty($assessments[$activity->assessmentid])) {
  763. $a = new stdclass();
  764. $a->id = $activity->assessmentid;
  765. $a->submissionid = $activity->submissionid;
  766. $a->submissiontitle = $activity->submissiontitle;
  767. $a->reviewerid = $activity->reviewerid;
  768. $a->timemodified = $activity->assessmentmodified;
  769. if ($activity->reviewerid == $USER->id || has_capability('mod/workshop:viewreviewernames', $context)) {
  770. $a->reviewernamevisible = true;
  771. } else {
  772. $a->reviewernamevisible = false;
  773. }
  774. // the following do-while wrapper allows to break from deeply nested if-statements
  775. do {
  776. if ($a->reviewerid === $USER->id) {
  777. // own assessments always visible
  778. $assessments[$activity->assessmentid] = $a;
  779. break;
  780. }
  781. if (has_capability('mod/workshop:viewallassessments', $context)) {
  782. if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) {
  783. if (isguestuser()) {
  784. // shortcut - guest user does not belong into any group
  785. break;
  786. }
  787. // this might be slow - show only submissions by users who share group with me in this cm
  788. if (!$modinfo->get_groups($cm->groupingid)) {
  789. break;
  790. }
  791. $reviewersgroups = groups_get_all_groups($course->id, $a->reviewerid, $cm->groupingid);
  792. if (is_array($reviewersgroups)) {
  793. $reviewersgroups = array_keys($reviewersgroups);
  794. $intersect = array_intersect($reviewersgroups, $modinfo->get_groups($cm->groupingid));
  795. if (empty($intersect)) {
  796. break;
  797. } else {
  798. // can see all assessments and shares a group with the reviewer
  799. $assessments[$activity->assessmentid] = $a;
  800. break;
  801. }
  802. }
  803. } else {
  804. // can see all assessments from all groups
  805. $assessments[$activity->assessmentid] = $a;
  806. }
  807. }
  808. } while (0);
  809. }
  810. }
  811. $rs->close();
  812. $workshopname = format_string($cm->name, true);
  813. if ($grader) {
  814. require_once($CFG->libdir.'/gradelib.php');
  815. $grades = grade_get_grades($courseid, 'mod', 'workshop', $cm->instance, array_keys($users));
  816. }
  817. foreach ($submissions as $submission) {
  818. $tmpactivity = new stdclass();
  819. $tmpactivity->type = 'workshop';
  820. $tmpactivity->cmid = $cm->id;
  821. $tmpactivity->name = $workshopname;
  822. $tmpactivity->sectionnum = $cm->sectionnum;
  823. $tmpactivity->timestamp = $submission->timemodified;
  824. $tmpactivity->subtype = 'submission';
  825. $tmpactivity->content = $submission;
  826. if ($grader) {
  827. $tmpactivity->grade = $grades->items[0]->grades[$submission->authorid]->str_long_grade;
  828. }
  829. if ($submission->authornamevisible and !empty($users[$submission->authorid])) {
  830. $tmpactivity->user = $users[$submission->authorid];
  831. }
  832. $activities[$index++] = $tmpactivity;
  833. }
  834. foreach ($assessments as $assessment) {
  835. $tmpactivity = new stdclass();
  836. $tmpactivity->type = 'workshop';
  837. $tmpactivity->cmid = $cm->id;
  838. $tmpactivity->name = $workshopname;
  839. $tmpactivity->sectionnum = $cm->sectionnum;
  840. $tmpactivity->timestamp = $assessment->timemodified;
  841. $tmpactivity->subtype = 'assessment';
  842. $tmpactivity->content = $assessment;
  843. if ($grader) {
  844. $tmpactivity->grade = $grades->items[1]->grades[$assessment->reviewerid]->str_long_grade;
  845. }
  846. if ($assessment->reviewernamevisible and !empty($users[$assessment->reviewerid])) {
  847. $tmpactivity->user = $users[$assessment->reviewerid];
  848. }
  849. $activities[$index++] = $tmpactivity;
  850. }
  851. }
  852. /**
  853. * Print single activity item prepared by {@see workshop_get_recent_mod_activity()}
  854. */
  855. function workshop_print_recent_mod_activity($activity, $courseid, $detail, $modnames, $viewfullnames) {
  856. global $CFG, $OUTPUT;
  857. if (!empty($activity->user)) {
  858. echo html_writer::tag('div', $OUTPUT->user_picture($activity->user, array('courseid'=>$courseid)),
  859. array('style' => 'float: left; padding: 7px;'));
  860. }
  861. if ($activity->subtype == 'submission') {
  862. echo html_writer::start_tag('div', array('class'=>'submission', 'style'=>'padding: 7px; float:left;'));
  863. if ($detail) {
  864. echo html_writer::start_tag('h4', array('class'=>'workshop'));
  865. $url = new moodle_url('/mod/workshop/view.php', array('id'=>$activity->cmid));
  866. $name = s($activity->name);
  867. echo $OUTPUT->image_icon('icon', $name, $activity->type);
  868. echo ' ' . $modnames[$activity->type];
  869. echo html_writer::link($url, $name, array('class'=>'name', 'style'=>'margin-left: 5px'));
  870. echo html_writer::end_tag('h4');
  871. }
  872. echo html_writer::start_tag('div', array('class'=>'title'));
  873. $url = new moodle_url('/mod/workshop/submission.php', array('cmid'=>$activity->cmid, 'id'=>$activity->content->id));
  874. $name = s($activity->content->title);
  875. echo html_writer::tag('strong', html_writer::link($url, $name));
  876. echo html_writer::end_tag('div');
  877. if (!empty($activity->user)) {
  878. echo html_writer::start_tag('div', array('class'=>'user'));
  879. $url = new moodle_url('/user/view.php', array('id'=>$activity->user->id, 'course'=>$courseid));
  880. $name = fullname($activity->user);
  881. $link = html_writer::link($url, $name);
  882. echo get_string('submissionby', 'workshop', $link);
  883. echo ' - '.userdate($activity->timestamp);
  884. echo html_writer::end_tag('div');
  885. } else {
  886. echo html_writer::start_tag('div', array('class'=>'anonymous'));
  887. echo get_string('submission', 'workshop');
  888. echo ' - '.userdate($activity->timestamp);
  889. echo html_writer::end_tag('div');
  890. }
  891. echo html_writer::end_tag('div');
  892. }
  893. if ($activity->subtype == 'assessment') {
  894. echo html_writer::start_tag('div', array('class'=>'assessment', 'style'=>'padding: 7px; float:left;'));
  895. if ($detail) {
  896. echo html_writer::start_tag('h4', array('class'=>'workshop'));
  897. $url = new moodle_url('/mod/workshop/view.php', array('id'=>$activity->cmid));
  898. $name = s($activity->name);
  899. echo $OUTPUT->image_icon('icon', $name, $activity->type);
  900. echo ' ' . $modnames[$activity->type];
  901. echo html_writer::link($url, $name, array('class'=>'name', 'style'=>'margin-left: 5px'));
  902. echo html_writer::end_tag('h4');
  903. }
  904. echo html_writer::start_tag('div', array('class'=>'title'));
  905. $url = new moodle_url('/mod/workshop/assessment.php', array('asid'=>$activity->content->id));
  906. $name = s($activity->content->submissiontitle);
  907. echo html_writer::tag('em', html_writer::link($url, $name));
  908. echo html_writer::end_tag('div');
  909. if (!empty($activity->user)) {
  910. echo html_writer::start_tag('div', array('class'=>'user'));
  911. $url = new moodle_url('/user/view.php', array('id'=>$activity->user->id, 'course'=>$courseid));
  912. $name = fullname($activity->user);
  913. $link = html_writer::link($url, $name);
  914. echo get_string('assessmentbyfullname', 'workshop', $link);
  915. echo ' - '.userdate($activity->timestamp);
  916. echo html_writer::end_tag('div');
  917. } else {
  918. echo html_writer::start_tag('div', array('class'=>'anonymous'));
  919. echo get_string('assessment', 'workshop');
  920. echo ' - '.userdate($activity->timestamp);
  921. echo html_writer::end_tag('div');
  922. }
  923. echo html_writer::end_tag('div');
  924. }
  925. echo html_writer::empty_tag('br', array('style'=>'clear:both'));
  926. }
  927. /**
  928. * @deprecated since Moodle 3.8
  929. */
  930. function workshop_scale_used() {
  931. throw new coding_exception('workshop_scale_used() can not be used anymore. Plugins can implement ' .
  932. '<modname>_scale_used_anywhere, all implementations of <modname>_scale_used are now ignored');
  933. }
  934. /**
  935. * Is a given scale used by any instance of workshop?
  936. *
  937. * The function asks all installed grading strategy subplugins. The workshop
  938. * core itself does not use scales. Both grade for submission and grade for
  939. * assessments do not use scales.
  940. *
  941. * @param int $scaleid id of the scale to check
  942. * @return bool
  943. */
  944. function workshop_scale_used_anywhere($scaleid) {
  945. global $CFG; // other files included from here
  946. $strategies = core_component::get_plugin_list('workshopform');
  947. foreach ($strategies as $strategy => $strategypath) {
  948. $strategylib = $strategypath . '/lib.php';
  949. if (is_readable($strategylib)) {
  950. require_once($strategylib);
  951. } else {
  952. throw new coding_exception('the grading forms subplugin must contain library ' . $strategylib);
  953. }
  954. $classname = 'workshop_' . $strategy . '_strategy';
  955. if (method_exists($classname, 'scale_used')) {
  956. if (call_user_func(array($classname, 'scale_used'), $scaleid)) {
  957. // no need to include any other files - scale is used
  958. return true;
  959. }
  960. }
  961. }
  962. return false;
  963. }
  964. ////////////////////////////////////////////////////////////////////////////////
  965. // Gradebook API //
  966. ////////////////////////////////////////////////////////////////////////////////
  967. /**
  968. * Creates or updates grade items for the give workshop instance
  969. *
  970. * Needed by grade_update_mod_grades() in lib/gradelib.php. Also used by
  971. * {@link workshop_update_grades()}.
  972. *
  973. * @param stdClass $workshop instance object with extra cmidnumber property
  974. * @param stdClass $submissiongrades data for the first grade item
  975. * @param stdClass $assessmentgrades data for the second grade item
  976. * @return void
  977. */
  978. function workshop_grade_item_update(stdclass $workshop, $submissiongrades=null, $assessmentgrades=null) {
  979. global $CFG;
  980. require_once($CFG->libdir.'/gradelib.php');
  981. $a = new stdclass();
  982. $a->workshopname = clean_param($workshop->name, PARAM_NOTAGS);
  983. $item = array();
  984. $item['itemname'] = get_string('gradeitemsubmission', 'workshop', $a);
  985. $item['gradetype'] = GRADE_TYPE_VALUE;
  986. $item['grademax'] = $workshop->grade;
  987. $item['grademin'] = 0;
  988. grade_update('mod/works…

Large files files are truncated, but you can click here to view the full file