diff --git a/allposts.php b/allposts.php index 87fe594..d79c362 100644 --- a/allposts.php +++ b/allposts.php @@ -1,196 +1,196 @@ -. - -/** - * This page prints all non-private personal oublog posts - * - * @author Jenny Gray - * @package oublog - */ - -require_once('../../config.php'); -require_once('locallib.php'); - -$tag = optional_param('tag', null, PARAM_TAG); // Tag to display. -$page = optional_param('page', 0, PARAM_INT); - -if (!$oublog = $DB->get_record("oublog", array("global"=>1))) { // The personal blogs module. - print_error('personalblognotsetup', 'oublog'); -} - -if (!$cm = get_coursemodule_from_instance('oublog', $oublog->id)) { - print_error('invalidcoursemodule'); -} - -if (!$course = $DB->get_record("course", array("id" => $cm->course))) { - print_error('coursemisconf'); -} - -$postperpage = $oublog->postperpage; -$offset = $page * $postperpage; -$url = new moodle_url('/mod/oublog/allposts.php', array( - 'page' => $page, - 'tag' => $tag)); -$PAGE->set_url($url); - -$context = context_module::instance($cm->id); -if (!empty($CFG->oublogallpostslogin) && $oublog->maxvisibility == OUBLOG_VISIBILITY_PUBLIC) { - // Set blog visibility temporarily to loggedin user to force login to this page. - $oublog->maxvisibility = OUBLOG_VISIBILITY_LOGGEDINUSER; - oublog_check_view_permissions($oublog, $context, $cm); - $oublog->maxvisibility = OUBLOG_VISIBILITY_PUBLIC; -} else { - oublog_check_view_permissions($oublog, $context, $cm); -} - -$oublogoutput = $PAGE->get_renderer('mod_oublog'); - -// Check security. -$blogtype = 'personal'; -$returnurl = 'allposts.php?'; - -if ($tag) { - $returnurl .= '&tag='.urlencode($tag); -} - -$canmanageposts = has_capability('mod/oublog:manageposts', $context); -$canaudit = has_capability('mod/oublog:audit', $context); - -// Log view all site entries event. -$params = array( - 'context' => $context, - 'objectid' => $oublog->id, - 'other' => array( - 'pageid' => $page - ) -); -$event = \mod_oublog\event\site_entries_viewed::create($params); -$event->add_record_snapshot('course_modules', $cm); -$event->add_record_snapshot('course', $course); -$event->trigger(); - -// Get strings. -$stroublog = get_string('modulename', 'oublog'); -$strnewposts = get_string('newerposts', 'oublog'); -$strolderposts = get_string('olderposts', 'oublog'); -$strfeeds = get_string('feeds', 'oublog'); - -$strblogsearch = get_string('searchblogs', 'oublog'); - -// Get Posts. -list($posts, $recordcount) = oublog_get_posts($oublog, $context, $offset, $cm, null, -1, null, - $tag, $canaudit, true); - -$PAGE->set_title(format_string($oublog->name)); -$PAGE->set_heading(format_string($course->fullname)); -$PAGE->navbar->add(format_string($oublog->name), new moodle_url('/mod/oublog/allposts.php')); -$CFG->additionalhtmlhead .= oublog_get_meta_tags($oublog, 'all', '', $cm); - -// Generate extra navigation. -if ($offset) { - $a = new stdClass(); - $a->from = ($offset+1); - $a->to = (($recordcount - $offset) > $postperpage) ? $offset + $postperpage : $recordcount; - $PAGE->navbar->add(get_string('extranavolderposts', 'oublog', $a)); -} else if (!empty($tag)) { - $PAGE->navbar->add(get_string('extranavtag', 'oublog', $tag)); -} - -if (oublog_search_installed()) { - $buttontext=<<
- - - -
-EOF; -} else { - $buttontext=''; -} -$url = new moodle_url("$CFG->wwwroot/course/mod.php", - array('update' => $cm->id, 'return' => true, 'sesskey' => sesskey())); -$PAGE->set_button($buttontext); - -// The right column, BEFORE the middle-column. -if (isloggedin() and !isguestuser()) { - list($oublog, $oubloginstance) = oublog_get_personal_blog($USER->id); - $blogeditlink = "
$oubloginstance->name"; - $bc = new block_contents(); - $bc->attributes['id'] = 'oublog-links'; - $bc->attributes['class'] = 'oublog-sideblock block'; - $bc->title = format_string($oublog->name); - $bc->content = $blogeditlink; - $PAGE->blocks->add_fake_block($bc, BLOCK_POS_RIGHT); -} - -if ($oublog->statblockon) { - // 'Discovery' block. - $stats = array(); - $stats[] = oublog_stats_output_participation($oublog, $cm, $oublogoutput, $course, true); - $stats[] = oublog_stats_output_commentpoststats($oublog, $cm, $oublogoutput, false, null, null,true); - $stats[] = oublog_stats_output_visitstats($oublog, $cm, $oublogoutput); - $stats[] = oublog_stats_output_poststats($oublog, $cm, $oublogoutput); - $stats[] = oublog_stats_output_commentstats($oublog, $cm, $oublogoutput); - $stats = $oublogoutput->render_stats_container('allposts', $stats); - $bc = new block_contents(); - $bc->attributes['id'] = 'oublog-discover'; - $bc->attributes['class'] = 'oublog-sideblock block'; - $bc->title = get_string('discovery', 'oublog', oublog_get_displayname($oublog, true)); - $bc->content = $stats; - if (!empty($stats)) { - $PAGE->blocks->add_fake_block($bc, BLOCK_POS_RIGHT); - } -} - -// Must be called after add_fake_blocks. -echo $OUTPUT->header(); -// Start main column. -print '
'; - -print $OUTPUT->skip_link_target(); - -// Renderer hook so extra info can be added to global blog pages in theme. -echo $oublogoutput->render_viewpage_prepost(); - -// Print blog posts. -if ($posts) { - echo "
"; - echo $OUTPUT->paging_bar($recordcount, $page, $postperpage, $returnurl); - echo '
'; - echo '
'; - $rowcounter = 1; - foreach ($posts as $post) { - $post->row = $rowcounter; - echo $oublogoutput->render_post($cm, $oublog, $post, $returnurl, $blogtype, - $canmanageposts, $canaudit, true, false); - $rowcounter++; - } - echo "
"; - echo $OUTPUT->paging_bar($recordcount, $page, $postperpage, $returnurl); - echo '
'; -} - -// Print information allowing the user to log in if necessary, or letting -// them know if there are no posts in the blog. -if (!isloggedin() || isguestuser()) { - print '

' . get_string('maybehiddenposts', 'oublog', - (object) array('link' => 'bloglogin.php', 'name' => oublog_get_displayname($oublog))) . '

'; -} else if (!$posts) { - print '

' . get_string('noposts', 'oublog', oublog_get_displayname($oublog)) . '

'; -} -print '
'; -// Finish the page. -echo $OUTPUT->footer(); +. + +/** + * This page prints all non-private personal oublog posts + * + * @author Jenny Gray + * @package oublog + */ + +require_once('../../config.php'); +require_once('locallib.php'); + +$tag = optional_param('tag', null, PARAM_TAG); // Tag to display. +$page = optional_param('page', 0, PARAM_INT); + +if (!$oublog = $DB->get_record("oublog", array("global"=>1))) { // The personal blogs module. + print_error('personalblognotsetup', 'oublog'); +} + +if (!$cm = get_coursemodule_from_instance('oublog', $oublog->id)) { + print_error('invalidcoursemodule'); +} + +if (!$course = $DB->get_record("course", array("id" => $cm->course))) { + print_error('coursemisconf'); +} + +$postperpage = $oublog->postperpage; +$offset = $page * $postperpage; +$url = new moodle_url('/mod/oublog/allposts.php', array( + 'page' => $page, + 'tag' => $tag)); +$PAGE->set_url($url); + +$context = context_module::instance($cm->id); +if (!empty($CFG->oublogallpostslogin) && $oublog->maxvisibility == OUBLOG_VISIBILITY_PUBLIC) { + // Set blog visibility temporarily to loggedin user to force login to this page. + $oublog->maxvisibility = OUBLOG_VISIBILITY_LOGGEDINUSER; + oublog_check_view_permissions($oublog, $context, $cm); + $oublog->maxvisibility = OUBLOG_VISIBILITY_PUBLIC; +} else { + oublog_check_view_permissions($oublog, $context, $cm); +} + +$oublogoutput = $PAGE->get_renderer('mod_oublog'); + +// Check security. +$blogtype = 'personal'; +$returnurl = 'allposts.php?'; + +if ($tag) { + $returnurl .= '&tag='.urlencode($tag); +} + +$canmanageposts = has_capability('mod/oublog:manageposts', $context); +$canaudit = has_capability('mod/oublog:audit', $context); + +// Log view all site entries event. +$params = array( + 'context' => $context, + 'objectid' => $oublog->id, + 'other' => array( + 'pageid' => $page + ) +); +$event = \mod_oublog\event\site_entries_viewed::create($params); +$event->add_record_snapshot('course_modules', $cm); +$event->add_record_snapshot('course', $course); +$event->trigger(); + +// Get strings. +$stroublog = get_string('modulename', 'oublog'); +$strnewposts = get_string('newerposts', 'oublog'); +$strolderposts = get_string('olderposts', 'oublog'); +$strfeeds = get_string('feeds', 'oublog'); + +$strblogsearch = get_string('searchblogs', 'oublog'); + +// Get Posts. +list($posts, $recordcount) = oublog_get_posts($oublog, $context, $offset, $cm, null, -1, null, + $tag, $canaudit, true); + +$PAGE->set_title(format_string($oublog->name)); +$PAGE->set_heading(format_string($course->fullname)); +$PAGE->navbar->add(format_string($oublog->name), new moodle_url('/mod/oublog/allposts.php')); +$CFG->additionalhtmlhead .= oublog_get_meta_tags($oublog, 'all', '', $cm); + +// Generate extra navigation. +if ($offset) { + $a = new stdClass(); + $a->from = ($offset+1); + $a->to = (($recordcount - $offset) > $postperpage) ? $offset + $postperpage : $recordcount; + $PAGE->navbar->add(get_string('extranavolderposts', 'oublog', $a)); +} else if (!empty($tag)) { + $PAGE->navbar->add(get_string('extranavtag', 'oublog', $tag)); +} + +if (oublog_search_installed()) { + $buttontext=<<
+ + + +
+EOF; +} else { + $buttontext=''; +} +$url = new moodle_url("$CFG->wwwroot/course/mod.php", + array('update' => $cm->id, 'return' => true, 'sesskey' => sesskey())); +$PAGE->set_button($buttontext); + +// The right column, BEFORE the middle-column. +if (isloggedin() and !isguestuser()) { + list($oublog, $oubloginstance) = oublog_get_personal_blog($USER->id); + $blogeditlink = "
$oubloginstance->name"; + $bc = new block_contents(); + $bc->attributes['id'] = 'oublog-links'; + $bc->attributes['class'] = 'oublog-sideblock block'; + $bc->title = format_string($oublog->name); + $bc->content = $blogeditlink; + $PAGE->blocks->add_fake_block($bc, BLOCK_POS_RIGHT); +} + +if ($oublog->statblockon) { + // 'Discovery' block. + $stats = array(); + $stats[] = oublog_stats_output_participation($oublog, $cm, $oublogoutput, $course, true); + $stats[] = oublog_stats_output_commentpoststats($oublog, $cm, $oublogoutput, false, null, null,true); + $stats[] = oublog_stats_output_visitstats($oublog, $cm, $oublogoutput); + $stats[] = oublog_stats_output_poststats($oublog, $cm, $oublogoutput); + $stats[] = oublog_stats_output_commentstats($oublog, $cm, $oublogoutput); + $stats = $oublogoutput->render_stats_container('allposts', $stats); + $bc = new block_contents(); + $bc->attributes['id'] = 'oublog-discover'; + $bc->attributes['class'] = 'oublog-sideblock block'; + $bc->title = get_string('discovery', 'oublog', oublog_get_displayname($oublog, true)); + $bc->content = $stats; + if (!empty($stats)) { + $PAGE->blocks->add_fake_block($bc, BLOCK_POS_RIGHT); + } +} + +// Must be called after add_fake_blocks. +echo $OUTPUT->header(); +// Start main column. +print '
'; + +print $OUTPUT->skip_link_target(); + +// Renderer hook so extra info can be added to global blog pages in theme. +echo $oublogoutput->render_viewpage_prepost(); + +// Print blog posts. +if ($posts) { + echo "
"; + echo $OUTPUT->paging_bar($recordcount, $page, $postperpage, $returnurl); + echo '
'; + echo '
'; + $rowcounter = 1; + foreach ($posts as $post) { + $post->row = $rowcounter; + echo $oublogoutput->render_post($cm, $oublog, $post, $returnurl, $blogtype, + $canmanageposts, $canaudit, true, false); + $rowcounter++; + } + echo "
"; + echo $OUTPUT->paging_bar($recordcount, $page, $postperpage, $returnurl); + echo '
'; +} + +// Print information allowing the user to log in if necessary, or letting +// them know if there are no posts in the blog. +if (!isloggedin() || isguestuser()) { + print '

' . get_string('maybehiddenposts', 'oublog', + (object) array('link' => 'bloglogin.php', 'name' => oublog_get_displayname($oublog))) . '

'; +} else if (!$posts) { + print '

' . get_string('noposts', 'oublog', oublog_get_displayname($oublog)) . '

'; +} +print '
'; +// Finish the page. +echo $OUTPUT->footer(); diff --git a/atomlib.php b/atomlib.php index 6980b97..4242a04 100644 --- a/atomlib.php +++ b/atomlib.php @@ -163,7 +163,7 @@ function atom_end_tag($tag, $level = 0, $endline = true) { // Return the start tag, the contents and the end tag. -function atom_full_tag($tag, $level = 0, $endline = true, $content, $attributes = null) { +function atom_full_tag($tag, $level, $endline, $content, $attributes = null) { global $CFG; $st = atom_start_tag($tag, $level, $endline, $attributes); if ($content === false) { diff --git a/classes/completion/custom_completion.php b/classes/completion/custom_completion.php index 0267151..141332d 100644 --- a/classes/completion/custom_completion.php +++ b/classes/completion/custom_completion.php @@ -61,7 +61,24 @@ public static function get_defined_custom_rules(): array { * @return array */ public function get_custom_rule_descriptions(): array { - return []; + global $DB; + $completionposts = $this->cm->customdata->customcompletionrules['completionposts'] ?? 0; + $completioncomments = $this->cm->customdata->customcompletionrules['completioncomments'] ?? 0; + + // Get oublog details + if (!($oublog = $DB->get_record('oublog', array('id' => $this->cm->instance)))) { + throw new \moodle_exception("Can't find oublog {$this->cm->instance}"); + } + + $a = (object) [ + 'number' => $completionposts, + 'name' => oublog_get_displayname($oublog) + ]; + + return [ + 'completionposts' => get_string('completiondetail:posts', 'oublog', $a), + 'completioncomments' => get_string('completiondetail:comments', 'oublog', $completioncomments), + ]; } /** diff --git a/classes/search/comments.php b/classes/search/comments.php index 629a273..042891c 100644 --- a/classes/search/comments.php +++ b/classes/search/comments.php @@ -211,7 +211,7 @@ public function attach_files($document) { $files = array(); foreach (self::FILEAREA as $area) { $files = array_merge($files, $fs->get_area_files($document->get('contextid'), $this->componentname, $area, - $document->get('itemid'), 'sortorder DESC, id ASC')); + $document->get('itemid'), 'sortorder DESC, id ASC', false)); } foreach ($files as $file) { diff --git a/classes/search/post.php b/classes/search/post.php index c9b36b4..b0cbad9 100644 --- a/classes/search/post.php +++ b/classes/search/post.php @@ -226,7 +226,7 @@ public function attach_files($document) { $files = array(); foreach (self::FILEAREA as $area) { $files = array_merge($files, $fs->get_area_files($document->get('contextid'), $this->componentname, $area, - $document->get('itemid'), 'sortorder DESC, id ASC')); + $document->get('itemid'), 'sortorder DESC, id ASC', false)); } foreach ($files as $file) { diff --git a/externallib.php b/externallib.php index 100de8b..40cfcd0 100644 --- a/externallib.php +++ b/externallib.php @@ -379,7 +379,7 @@ public static function get_blog_posts2_parameters(): external_function_parameter * @param string $username - used to ensure user posts only * @return array of posts */ - public static function get_blog_posts($blogid, $bcontextid, $selected, $inccomments = false, $username) { + public static function get_blog_posts($blogid, $bcontextid, $selected, $inccomments, $username) { return self::get_blog_posts2($blogid, $bcontextid, $selected, $inccomments, self::convert_username($username)); } diff --git a/lang/en/oublog.php b/lang/en/oublog.php index 118b0c1..aa79ffc 100644 --- a/lang/en/oublog.php +++ b/lang/en/oublog.php @@ -233,6 +233,8 @@ $string['completioncommentsgroup'] = 'Require comments'; $string['completioncommentsgroup_help'] = 'If you enable this option, the blog will be marked as complete for a student once they have left the specified number of comments.'; $string['completioncomments'] = 'User must make comments on blog posts:'; +$string['completiondetail:posts'] = 'Make {$a->name} posts: {$a->number}'; +$string['completiondetail:comments'] = 'Make comments: {$a}'; $string['computingguide'] = 'Guide to OU blogs'; $string['computingguideurl'] = 'Computing guide URL'; diff --git a/lib.php b/lib.php index 5cb0c57..9a616a2 100644 --- a/lib.php +++ b/lib.php @@ -39,6 +39,7 @@ */ function oublog_add_instance($oublog) { global $DB; + $cmid = $oublog->coursemodule; // Generate an accesstoken. $oublog->accesstoken = md5(uniqid(rand(), true)); $oublog->timemodified = time(); @@ -62,6 +63,10 @@ function oublog_add_instance($oublog) { } oublog_grade_item_update($oublog); + // Update completion event in calendar. + $completionexpected = (!empty($oublog->completionexpected)) ? $oublog->completionexpected : null; + \core_completion\api::update_completion_date_event($cmid, 'oublog', $oublog->id, $completionexpected); + return($oublog->id); } @@ -77,6 +82,7 @@ function oublog_add_instance($oublog) { */ function oublog_update_instance($oublog) { global $DB; + $cmid = $oublog->coursemodule; $oublog->id = $oublog->instance; $oublog->timemodified = time(); if (!$DB->get_record('oublog', array('id' => $oublog->id))) { @@ -103,6 +109,10 @@ function oublog_update_instance($oublog) { oublog_grade_item_update($oublog); + // Update completion event in calendar. + $completionexpected = (!empty($oublog->completionexpected)) ? $oublog->completionexpected : null; + \core_completion\api::update_completion_date_event($cmid, 'oublog', $oublog->id, $completionexpected); + return(true); } @@ -167,6 +177,9 @@ function oublog_delete_instance($oublogid) { oublog_grade_item_delete($oublog); + // Delete event in calendar when deleting activity. + \core_completion\api::update_completion_date_event($cm->id, 'oublog', $oublogid, null); + // oublog return($DB->delete_records('oublog', array('id'=>$oublog->id))); @@ -1548,4 +1561,38 @@ function oublog_get_coursemodule_info($coursemodule) { } return $info; -} \ No newline at end of file +} + +/** + * This function receives a calendar event and returns the action associated with it, or null if there is none. + * + * This is used by block_myoverview in order to display the event appropriately. If null is returned then the event + * is not displayed on the block. + * + * @param calendar_event $event + * @param \core_calendar\action_factory $factory + * @param int $userid User id to use for all capability checks, etc. Set to 0 for current user (default). + * @return \core_calendar\local\event\entities\action_interface|null + */ +function mod_oublog_core_calendar_provide_event_action(calendar_event $event, \core_calendar\action_factory $factory, int $userid = 0) { + global $USER; + if (!$userid) { + $userid = $USER->id; + } + $cm = get_fast_modinfo($event->courseid, $userid)->instances['oublog'][$event->instance]; + if (!$cm->uservisible) { + // The module is not visible to the user for any reason. + return null; + } + $completion = new \completion_info($cm->get_course()); + $completiondata = $completion->get_data($cm, false, $userid); + if ($completiondata->completionstate != COMPLETION_INCOMPLETE) { + return null; + } + return $factory->create_instance( + get_string('view'), + new \moodle_url('/mod/oublog/view.php', ['id' => $cm->id]), + 1, + true + ); +} diff --git a/locallib.php b/locallib.php index fb4b1c2..97e09f3 100644 --- a/locallib.php +++ b/locallib.php @@ -635,7 +635,7 @@ function oublog_edit_post($post, $cm) { * @param string $sqlorder port sorting * @return mixed all data to print a list of blog posts */ -function oublog_get_posts($oublog, $context, $offset = 0, $cm, $groupid, $individualid = -1, +function oublog_get_posts($oublog, $context, $offset, $cm, $groupid, $individualid = -1, $userid = null, $tag = '', $canaudit = false, $ignoreprivate = null, $masterblog = null, $paginglimit = null, $sqlorder = '') { global $CFG, $USER, $DB; $params = array(); @@ -1144,7 +1144,7 @@ function oublog_get_tags($oublog, $groupid, $cm, $oubloginstanceid=null, $indivi * @param object $masterblog * @return array Tag cloud HTML, current filter tag */ -function oublog_get_tag_cloud($baseurl, $oublog, $groupid, $cm, $oubloginstanceid=null, $individualid=-1, $tagorder, +function oublog_get_tag_cloud($baseurl, $oublog, $groupid, $cm, $oubloginstanceid, $individualid, $tagorder, $masterblog = null, $limit = null) { global $PAGE; $cloud = ''; @@ -3401,7 +3401,7 @@ function oublog_can_grade($course, $oublog, $cm, $groupid=0) { * @param bool $showuseridentityfields show user identity fields. * @return array user participation */ -function oublog_get_participation($oublog, $context, $groupid = 0, $cm, +function oublog_get_participation($oublog, $context, $groupid, $cm, $course, $start = null, $end = null, $sort = 'u.firstname,u.lastname', $masterblog = null, $cmmaster = null, $coursemaster = null, $showuseridentityfields = false) { global $DB; @@ -3589,7 +3589,7 @@ function oublog_load_user_identity_data($context, $users) { * @return array user participation */ function oublog_get_user_participation($oublog, $context, - $userid, $groupid = 0, $cm, $course, $start = null, $end = null, + $userid, $groupid, $cm, $course, $start = null, $end = null, $getposts = true, $getcomments = true, $limitfrom = null, $limitnum = null, $getgrades = false, $masterblog = null, $cmmaster = null, $coursemaster = null) { global $DB; @@ -4504,7 +4504,7 @@ function oublog_stats_output_commentpoststats($oublog, $cm, $renderer = null, $a * @param object $coursemaster * @param bool $ajax true to return data object rather than html */ -function oublog_stats_output_myparticipation($oublog, $cm, $renderer = null, $course, $currentindividual, $globalindividual = null, +function oublog_stats_output_myparticipation($oublog, $cm, $renderer, $course, $currentindividual, $globalindividual = null, $masterblog = null, $cmmaster = null, $coursemaster = null) { global $PAGE, $DB, $USER, $OUTPUT; if (!isloggedin()) {// My participation is only visible to actual users. @@ -4620,7 +4620,7 @@ function oublog_stats_output_myparticipation($oublog, $cm, $renderer = null, $co * @param object $masterblog * @param bool $ return data object rather than html */ -function oublog_stats_output_participation($oublog, $cm, $renderer = null, $course, $allposts = false, $curindividual = -1, $globalindividual = null, +function oublog_stats_output_participation($oublog, $cm, $renderer, $course, $allposts = false, $curindividual = -1, $globalindividual = null, $masterblog = null) { global $PAGE, $DB, $USER, $OUTPUT; if (!$renderer) { diff --git a/mod_form.php b/mod_form.php index 833638e..c41862a 100644 --- a/mod_form.php +++ b/mod_form.php @@ -1,383 +1,385 @@ -. -/** - * Define the OU Blog module creation form - * - * @access Matt Clarkson - * @package oublog - */ -if (defined('OUBLOG_EDIT_INSTANCE')) { - - require_once($CFG->libdir.'/formslib.php'); - abstract class moodleform_mod extends moodleform { - } // Fake that we are using the moodleform_mod base class. - -} else { - require_once('moodleform_mod.php'); -} -require_once('locallib.php'); - -class mod_oublog_mod_form extends moodleform_mod { - - public function definition() { - - global $COURSE, $CFG; - $mform = $this->_form; - - $mform->addElement('header', 'general', get_string('general', 'form')); - - // Adding the standard "name" field. - $mform->addElement('text', 'name', get_string('blogname', 'oublog'), array('size'=>'64')); - $mform->setType('name', PARAM_TEXT); - $mform->addRule('name', null, 'required', null, 'client'); - $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client'); - - if (!defined('OUBLOG_EDIT_INSTANCE')) { - $this->standard_intro_elements(get_string('oublogintro', 'oublog')); - // Adding the "allowcomments" field. - $options = array(OUBLOG_COMMENTS_ALLOW => get_string('logincomments', 'oublog'), - OUBLOG_COMMENTS_ALLOWPUBLIC => get_string('publiccomments', 'oublog'), - OUBLOG_COMMENTS_PREVENT => get_string('nocomments', 'oublog')); - - $mform->addElement('select', 'allowcomments', get_string('allowcommentsmax', 'oublog'), $options); - $mform->setType('allowcomments', PARAM_INT); - $mform->addHelpButton('allowcomments', 'allowcomments', 'oublog'); - - // Adding the "individual" field. - $options = array(OUBLOG_NO_INDIVIDUAL_BLOGS => get_string('no_blogtogetheroringroups', 'oublog'), - OUBLOG_SEPARATE_INDIVIDUAL_BLOGS => get_string('separateindividualblogs', 'oublog'), - OUBLOG_VISIBLE_INDIVIDUAL_BLOGS => get_string('visibleindividualblogs', 'oublog')); - $mform->addElement('select', 'individual', get_string('individualblogs', 'oublog'), $options); - $mform->setType('individual', PARAM_INT); - $mform->setDefault('individual', OUBLOG_NO_INDIVIDUAL_BLOGS); - $mform->addHelpButton('individual', 'individualblogs', 'oublog'); - - // Disable "maxvisibility" field when "individual" field is set (not default). - $mform->disabledIf('maxvisibility', 'individual', OUBLOG_NO_INDIVIDUAL_BLOGS, OUBLOG_NO_INDIVIDUAL_BLOGS); - - // Adding the "maxvisibility" field. - $options = array(OUBLOG_VISIBILITY_COURSEUSER => get_string('visiblecourseusers', 'oublog'), - OUBLOG_VISIBILITY_LOGGEDINUSER => get_string('visibleloggedinusers', 'oublog'), - OUBLOG_VISIBILITY_PUBLIC => get_string('visiblepublic', 'oublog')); - - $haschild = false; - if (!empty($this->_cm->idnumber)) { - $haschild = !empty(oublog_get_children($this->_cm->idnumber)) ? true : false; - } - if ($haschild) { - $mform->addElement('text', 'idsharedblog', get_string('sharedblog', 'oublog'), array('disabled')); - } else { - // Enable "sharedblog" field when "individual" field is set. - $mform->disabledIf('idsharedblog', 'individual', 'eq', OUBLOG_NO_INDIVIDUAL_BLOGS); - $mform->addElement('text', 'idsharedblog', get_string('sharedblog', 'oublog')); - } - $mform->setType('idsharedblog', PARAM_TEXT); - // Have to add content later. - $mform->addHelpButton('idsharedblog', 'sharedblog', 'oublog'); - - $mform->addElement('select', 'maxvisibility', get_string('maxvisibility', 'oublog'), $options); - $mform->setType('maxvisibility', PARAM_INT); - $mform->addHelpButton('maxvisibility', 'maxvisibility', 'oublog'); - - // Whether intro text shows on post form pages. - $mform->addElement('checkbox', 'introonpost', get_string('introonpost', 'oublog'), '', 0); - - // Max size of attachments. - $modulesettings = get_config('mod_oublog'); - $choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes); - $mform->addElement('select', 'maxbytes', - get_string('maxattachmentsize', 'oublog'), $choices); - $mform->addHelpButton('maxbytes', 'maxattachmentsize', 'oublog'); - $mform->setDefault('maxbytes', $modulesettings->maxbytes); - - // Max number of attachments. - $choices = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20 => 20, 50 => 50, 100 => 100); - $mform->addElement('select', 'maxattachments', - get_string('maxattachments', 'oublog'), $choices); - $mform->addHelpButton('maxattachments', 'maxattachments', 'oublog'); - $mform->setDefault('maxattachments', $modulesettings->maxattachments); - - // Number of post per page. - $choices = array(25 => 25, 50 => 50, 75 => 75, 100 => 100); - $mform->addElement('select', 'postperpage', - get_string('numberofposts', 'oublog'), $choices); - $mform->addHelpButton('postperpage', 'numberofposts', 'oublog'); - $mform->setDefault('postperpage', 25); - - // Show OU Alerts reporting link. - if (oublog_oualerts_enabled()) { - $mform->addElement('text', 'reportingemail', get_string('reportingemail', 'oublog'), - array('size'=>'48')); - $mform->addHelpButton('reportingemail', 'reportingemail', 'oublog'); - $mform->setType('reportingemail', PARAM_NOTAGS); - $mform->addRule('reportingemail', get_string('maximumchars', '', 255), - 'maxlength', 255, 'client'); - } - - $mform->addElement('header', 'advanced', get_string('advancedoptions', 'oublog')); - - // Enable the stats block. - $mform->addElement('checkbox', 'statblockon', get_string('statblockon', 'oublog'), '', 0); - $mform->addHelpButton('statblockon', 'statblockon', 'oublog'); - - $mform->addElement('text', 'displayname', get_string('displayname', 'oublog'), - array('size'=>'48')); - $mform->addHelpButton('displayname', 'displayname', 'oublog'); - $mform->setType('displayname', PARAM_NOTAGS); - $mform->addRule('displayname', get_string('maximumchars', '', 255), - 'maxlength', 255, 'client'); - - $mform->addElement('checkbox', 'allowimport', get_string('allowimport', 'oublog'), '', 0); - $mform->addHelpButton('allowimport', 'allowimport', 'oublog'); - - $mform->addElement('header', 'tagheading', get_string('tags', 'oublog')); - - $mform->addElement('text', 'tagslist', get_string('tags', 'oublog'), - array('size'=>'48')); - $mform->addHelpButton('tagslist', 'predefinedtags', 'oublog'); - $mform->setType('tagslist', PARAM_TAGLIST); - $mform->addRule('tagslist', get_string('maximumchars', '', 255), - 'maxlength', 255, 'client'); - - $tagopts = array( - '0' => get_string('none'), - '1' => get_string('restricttags_set', 'oublog'), - '2' => get_string('restricttags_req', 'oublog'), - '3' => get_string('restricttags_req_set', 'oublog'), - '4' => get_string('restricttags_default', 'oublog') - ); - $mform->addElement('select', 'restricttags', get_string('restricttags', 'oublog'), $tagopts); - $mform->addHelpButton('restricttags', 'restricttags', 'oublog'); - - $mform->addElement('header', 'limits', get_string('limits', 'oublog')); - - // Limiting post/comments dates. - $mform->addElement('date_time_selector', 'postfrom', - get_string('postfrom', 'oublog'), array('optional' => true)); - $mform->addElement('date_time_selector', 'postuntil', - get_string('postuntil', 'oublog'), array('optional' => true)); - $mform->addElement('date_time_selector', 'commentfrom', - get_string('commentfrom', 'oublog'), array('optional' => true)); - $mform->addElement('date_time_selector', 'commentuntil', - get_string('commentuntil', 'oublog'), array('optional' => true)); - - $mform->disabledIf('commentfrom', 'allowcomments', 'eq', 0); - $mform->disabledIf('commentuntil', 'allowcomments', 'eq', 0); - - $mform->addElement('header', 'modstandardgrade', get_string('gradenoun')); - // Adding the "grading" field. - $options = array(OUBLOG_NO_GRADING => get_string('nograde', 'oublog'), - OUBLOG_TEACHER_GRADING => get_string('teachergrading', 'oublog'), - OUBLOG_USE_RATING => get_string('userrating', 'oublog')); - $mform->addElement('select', 'grading', get_string('grading', 'oublog'), $options); - $mform->setType('grading', PARAM_INT); - $mform->addHelpButton('grading', 'grading', 'oublog'); - - $mform->addElement('modgrade', 'grade', get_string('gradenoun')); - $mform->addHelpButton('grade', 'modgrade', 'grades'); - $mform->setDefault('grade', $CFG->gradepointdefault); - $mform->disabledIf('grade', 'grading', 'ne', OUBLOG_TEACHER_GRADING); - - // Add standard elements, common to all modules. - $features = new stdClass; - $features->groupings = true; - $this->standard_coursemodule_elements($features); - } else { - // Adding the "summary" field. - $mform->addElement('editor', 'summary_editor', get_string('summary', 'oublog'), null, - array('maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes' => $CFG->maxbytes)); - $mform->setType('summary', PARAM_RAW); - $mform->addElement('hidden', 'instance'); - $mform->setType('instance', PARAM_INT); - } - - // Add standard buttons, common to all modules. - $this->add_action_buttons(); - - } - - public function add_completion_rules() { - $mform =& $this->_form; - - $group=array(); - $group[] =& $mform->createElement('checkbox', 'completionpostsenabled', ' ', get_string('completionposts', 'oublog')); - $group[] =& $mform->createElement('text', 'completionposts', ' ', array('size'=>3)); - $mform->setType('completionposts', PARAM_INT); - $mform->addGroup($group, 'completionpostsgroup', get_string('completionpostsgroup', 'oublog'), array(' '), false); - $mform->addHelpButton('completionpostsgroup', 'completionpostsgroup', 'oublog'); - $mform->disabledIf('completionposts', 'completionpostsenabled', 'notchecked'); - - $group=array(); - $group[] =& $mform->createElement('checkbox', 'completioncommentsenabled', ' ', get_string('completioncomments', 'oublog')); - $group[] =& $mform->createElement('text', 'completioncomments', ' ', array('size'=>3)); - $mform->setType('completioncomments', PARAM_INT); - $mform->addGroup($group, 'completioncommentsgroup', get_string('completioncommentsgroup', 'oublog'), array(' '), false); - $mform->addHelpButton('completioncommentsgroup', 'completioncommentsgroup', 'oublog'); - $mform->disabledIf('completioncomments', 'completioncommentsenabled', 'notchecked'); - - // Restriction for grade completion - $mform->disabledIf('completionusegrade', 'grade', 'eq', 0); - - return array('completionpostsgroup', 'completioncommentsgroup'); - } - - public function completion_rule_enabled($data) { - return ((!empty($data['completionpostsenabled']) && $data['completionposts']!=0)) || - ((!empty($data['completioncommentsenabled']) && $data['completioncomments']!=0)); - } - - public function get_data() { - $data=parent::get_data(); - if (!$data) { - return false; - } - // Turn off completion settings if the checkboxes aren't ticked - if (!empty($data->completionunlocked)) { - $autocompletion = !empty($data->completion) && $data->completion == COMPLETION_TRACKING_AUTOMATIC; - if (empty($data->completionpostsenabled) || !$autocompletion) { - $data->completionposts = 0; - } - if (empty($data->completioncommentsenabled) || !$autocompletion) { - $data->completioncomments = 0; - } - } - // If maxvisibility is disabled by individual mode, ensure it's limited to course. - if (isset($data->individual) && ($data->individual == OUBLOG_SEPARATE_INDIVIDUAL_BLOGS - || $data->individual == OUBLOG_VISIBLE_INDIVIDUAL_BLOGS)) { - $data->maxvisibility = OUBLOG_VISIBILITY_COURSEUSER; - } - // Set the reportingemail to null if empty so that we have consistency. - if (empty($data->reportingemail)) { - $data->reportingemail = null; - } - // Set statblockon to null if empty so that we have consistency. - if (empty($data->statblockon)) { - $data->statblockon = 0; - } - if (empty($data->displayname)) { - $data->displayname = null; - } - if (empty($data->allowimport)) { - $data->allowimport = 0; - } - if (empty($data->introonpost)) { - $data->introonpost = 0; - } - if (!empty($data->tagslist)) { - $data->tagslist = trim($data->tagslist); - } - if (empty($data->restricttags)) { - $data->restricttags = 0; - } - if (empty($data->postfrom)) { - $data->postfrom = 0; - } - if (empty($data->postuntil)) { - $data->postuntil = 0; - } - if (empty($data->commentfrom)) { - $data->commentfrom = 0; - } - if (empty($data->commentuntil)) { - $data->commentuntil = 0; - } - if (isset($data->grading) && $data->grading == OUBLOG_NO_GRADING) { - // Unset grade if grading turned off. - $data->grade = 0; - } - return $data; - } - - public function data_preprocessing(&$default_values) { - // Set up the completion checkboxes which aren't part of standard data. - // We also make the default value (if you turn on the checkbox) for those - // numbers to be 1, this will not apply unless checkbox is ticked. - $default_values['completionpostsenabled']= - !empty($default_values['completionposts']) ? 1 : 0; - if (empty($default_values['completionposts'])) { - $default_values['completionposts']=1; - } - $default_values['completioncommentsenabled']= - !empty($default_values['completioncomments']) ? 1 : 0; - if (empty($default_values['completioncomments'])) { - $default_values['completioncomments']=1; - } - } - - public function validation($data, $files) { - global $DB; - $errors = parent::validation($data, $files); - if (!empty($data['groupmode']) && isset($data['allowcomments']) && - $data['allowcomments'] == OUBLOG_COMMENTS_ALLOWPUBLIC) { - $errors['allowcomments'] = get_string('error_grouppubliccomments', 'oublog'); - } - if (!empty($data['reportingemail'])) { - $emails = explode(',', trim($data['reportingemail'])); - foreach ($emails as $email) { - if (!validate_email($email)) { - $errors['reportingemail'] = get_string('invalidemail', 'forumng'); - } - } - } - if (!empty($data['allowimport']) && $data['individual'] == OUBLOG_NO_INDIVIDUAL_BLOGS) { - // Can only import on individual or global blogs. - if (!empty($data['instance'])) { - if (!$DB->get_field('oublog', 'global', array('id' => $data['instance']))) { - $errors['allowimport'] = get_string('allowimport_invalid', 'oublog'); - } - } else { - $errors['allowimport'] = get_string('allowimport_invalid', 'oublog'); - } - } - // If form is on blog edit page rather than the blog options edit instance page. - if (isset($data['grading'])) { - if (($data['grading'] == OUBLOG_TEACHER_GRADING && empty($data['grade'])) || - ($data['grading'] == OUBLOG_USE_RATING && empty($data['assessed']))) { - $errors['grading'] = get_string('grading_invalid', 'oublog'); - } - } - if (isset($data['restricttags']) && empty($data['tagslist']) - && ($data['restricttags'] == 1 || $data['restricttags'] == 3)) { - // When forcing use of pre-defined tags must define some. - $errors['tagslist'] = get_string('required'); - } - if (!empty($data['idsharedblog'])) { - $masterblog = oublog_get_master($data['idsharedblog'], false); - - // Cannot get master if it doesn't have ID number or has more than 1. - if (empty($masterblog)) { - $errors['idsharedblog'] = get_string('sharedblog_invalid', 'oublog'); - } - if (count($masterblog) > 1) { - $errors['idsharedblog'] = get_string('sharedblog_invalid_morethan1', 'oublog'); - } - // Cannot be child if it is master. - if (!empty($data['cmidnumber'])) { - $validatemasterblog = oublog_get_children($data['cmidnumber']); - if (!empty($validatemasterblog)) { - $errors['idsharedblog'] = get_string('sharedblog_mastered', 'oublog'); - } - } - - $masterblog = reset($masterblog); - // Cannot be master blog if it already child of the other blog. - if (!empty($masterblog->idsharedblog)) { - $errors['idsharedblog'] = get_string('sharedblog_existed', 'oublog'); - } - } - return $errors; - } -} +. +/** + * Define the OU Blog module creation form + * + * @access Matt Clarkson + * @package oublog + */ +if (defined('OUBLOG_EDIT_INSTANCE')) { + + require_once($CFG->libdir.'/formslib.php'); + abstract class mod_oublog_mod_form_base extends moodleform { + } // Fake that we are using the moodleform_mod base class. + +} else { + require_once('moodleform_mod.php'); + abstract class mod_oublog_mod_form_base extends moodleform_mod { + } +} +require_once('locallib.php'); + +class mod_oublog_mod_form extends mod_oublog_mod_form_base { + + public function definition() { + + global $COURSE, $CFG; + $mform = $this->_form; + + $mform->addElement('header', 'general', get_string('general', 'form')); + + // Adding the standard "name" field. + $mform->addElement('text', 'name', get_string('blogname', 'oublog'), array('size'=>'64')); + $mform->setType('name', PARAM_TEXT); + $mform->addRule('name', null, 'required', null, 'client'); + $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client'); + + if (!defined('OUBLOG_EDIT_INSTANCE')) { + $this->standard_intro_elements(get_string('oublogintro', 'oublog')); + // Adding the "allowcomments" field. + $options = array(OUBLOG_COMMENTS_ALLOW => get_string('logincomments', 'oublog'), + OUBLOG_COMMENTS_ALLOWPUBLIC => get_string('publiccomments', 'oublog'), + OUBLOG_COMMENTS_PREVENT => get_string('nocomments', 'oublog')); + + $mform->addElement('select', 'allowcomments', get_string('allowcommentsmax', 'oublog'), $options); + $mform->setType('allowcomments', PARAM_INT); + $mform->addHelpButton('allowcomments', 'allowcomments', 'oublog'); + + // Adding the "individual" field. + $options = array(OUBLOG_NO_INDIVIDUAL_BLOGS => get_string('no_blogtogetheroringroups', 'oublog'), + OUBLOG_SEPARATE_INDIVIDUAL_BLOGS => get_string('separateindividualblogs', 'oublog'), + OUBLOG_VISIBLE_INDIVIDUAL_BLOGS => get_string('visibleindividualblogs', 'oublog')); + $mform->addElement('select', 'individual', get_string('individualblogs', 'oublog'), $options); + $mform->setType('individual', PARAM_INT); + $mform->setDefault('individual', OUBLOG_NO_INDIVIDUAL_BLOGS); + $mform->addHelpButton('individual', 'individualblogs', 'oublog'); + + // Disable "maxvisibility" field when "individual" field is set (not default). + $mform->disabledIf('maxvisibility', 'individual', OUBLOG_NO_INDIVIDUAL_BLOGS, OUBLOG_NO_INDIVIDUAL_BLOGS); + + // Adding the "maxvisibility" field. + $options = array(OUBLOG_VISIBILITY_COURSEUSER => get_string('visiblecourseusers', 'oublog'), + OUBLOG_VISIBILITY_LOGGEDINUSER => get_string('visibleloggedinusers', 'oublog'), + OUBLOG_VISIBILITY_PUBLIC => get_string('visiblepublic', 'oublog')); + + $haschild = false; + if (!empty($this->_cm->idnumber)) { + $haschild = !empty(oublog_get_children($this->_cm->idnumber)) ? true : false; + } + if ($haschild) { + $mform->addElement('text', 'idsharedblog', get_string('sharedblog', 'oublog'), array('disabled')); + } else { + // Enable "sharedblog" field when "individual" field is set. + $mform->disabledIf('idsharedblog', 'individual', 'eq', OUBLOG_NO_INDIVIDUAL_BLOGS); + $mform->addElement('text', 'idsharedblog', get_string('sharedblog', 'oublog')); + } + $mform->setType('idsharedblog', PARAM_TEXT); + // Have to add content later. + $mform->addHelpButton('idsharedblog', 'sharedblog', 'oublog'); + + $mform->addElement('select', 'maxvisibility', get_string('maxvisibility', 'oublog'), $options); + $mform->setType('maxvisibility', PARAM_INT); + $mform->addHelpButton('maxvisibility', 'maxvisibility', 'oublog'); + + // Whether intro text shows on post form pages. + $mform->addElement('checkbox', 'introonpost', get_string('introonpost', 'oublog'), '', 0); + + // Max size of attachments. + $modulesettings = get_config('mod_oublog'); + $choices = get_max_upload_sizes($CFG->maxbytes, $COURSE->maxbytes); + $mform->addElement('select', 'maxbytes', + get_string('maxattachmentsize', 'oublog'), $choices); + $mform->addHelpButton('maxbytes', 'maxattachmentsize', 'oublog'); + $mform->setDefault('maxbytes', $modulesettings->maxbytes); + + // Max number of attachments. + $choices = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20 => 20, 50 => 50, 100 => 100); + $mform->addElement('select', 'maxattachments', + get_string('maxattachments', 'oublog'), $choices); + $mform->addHelpButton('maxattachments', 'maxattachments', 'oublog'); + $mform->setDefault('maxattachments', $modulesettings->maxattachments); + + // Number of post per page. + $choices = array(25 => 25, 50 => 50, 75 => 75, 100 => 100); + $mform->addElement('select', 'postperpage', + get_string('numberofposts', 'oublog'), $choices); + $mform->addHelpButton('postperpage', 'numberofposts', 'oublog'); + $mform->setDefault('postperpage', 25); + + // Show OU Alerts reporting link. + if (oublog_oualerts_enabled()) { + $mform->addElement('text', 'reportingemail', get_string('reportingemail', 'oublog'), + array('size'=>'48')); + $mform->addHelpButton('reportingemail', 'reportingemail', 'oublog'); + $mform->setType('reportingemail', PARAM_NOTAGS); + $mform->addRule('reportingemail', get_string('maximumchars', '', 255), + 'maxlength', 255, 'client'); + } + + $mform->addElement('header', 'advanced', get_string('advancedoptions', 'oublog')); + + // Enable the stats block. + $mform->addElement('checkbox', 'statblockon', get_string('statblockon', 'oublog'), '', 0); + $mform->addHelpButton('statblockon', 'statblockon', 'oublog'); + + $mform->addElement('text', 'displayname', get_string('displayname', 'oublog'), + array('size'=>'48')); + $mform->addHelpButton('displayname', 'displayname', 'oublog'); + $mform->setType('displayname', PARAM_NOTAGS); + $mform->addRule('displayname', get_string('maximumchars', '', 255), + 'maxlength', 255, 'client'); + + $mform->addElement('checkbox', 'allowimport', get_string('allowimport', 'oublog'), '', 0); + $mform->addHelpButton('allowimport', 'allowimport', 'oublog'); + + $mform->addElement('header', 'tagheading', get_string('tags', 'oublog')); + + $mform->addElement('text', 'tagslist', get_string('tags', 'oublog'), + array('size'=>'48')); + $mform->addHelpButton('tagslist', 'predefinedtags', 'oublog'); + $mform->setType('tagslist', PARAM_TAGLIST); + $mform->addRule('tagslist', get_string('maximumchars', '', 255), + 'maxlength', 255, 'client'); + + $tagopts = array( + '0' => get_string('none'), + '1' => get_string('restricttags_set', 'oublog'), + '2' => get_string('restricttags_req', 'oublog'), + '3' => get_string('restricttags_req_set', 'oublog'), + '4' => get_string('restricttags_default', 'oublog') + ); + $mform->addElement('select', 'restricttags', get_string('restricttags', 'oublog'), $tagopts); + $mform->addHelpButton('restricttags', 'restricttags', 'oublog'); + + $mform->addElement('header', 'limits', get_string('limits', 'oublog')); + + // Limiting post/comments dates. + $mform->addElement('date_time_selector', 'postfrom', + get_string('postfrom', 'oublog'), array('optional' => true)); + $mform->addElement('date_time_selector', 'postuntil', + get_string('postuntil', 'oublog'), array('optional' => true)); + $mform->addElement('date_time_selector', 'commentfrom', + get_string('commentfrom', 'oublog'), array('optional' => true)); + $mform->addElement('date_time_selector', 'commentuntil', + get_string('commentuntil', 'oublog'), array('optional' => true)); + + $mform->disabledIf('commentfrom', 'allowcomments', 'eq', 0); + $mform->disabledIf('commentuntil', 'allowcomments', 'eq', 0); + + $mform->addElement('header', 'modstandardgrade', get_string('gradenoun')); + // Adding the "grading" field. + $options = array(OUBLOG_NO_GRADING => get_string('nograde', 'oublog'), + OUBLOG_TEACHER_GRADING => get_string('teachergrading', 'oublog'), + OUBLOG_USE_RATING => get_string('userrating', 'oublog')); + $mform->addElement('select', 'grading', get_string('grading', 'oublog'), $options); + $mform->setType('grading', PARAM_INT); + $mform->addHelpButton('grading', 'grading', 'oublog'); + + $mform->addElement('modgrade', 'grade', get_string('gradenoun')); + $mform->addHelpButton('grade', 'modgrade', 'grades'); + $mform->setDefault('grade', $CFG->gradepointdefault); + $mform->disabledIf('grade', 'grading', 'ne', OUBLOG_TEACHER_GRADING); + + // Add standard elements, common to all modules. + $features = new stdClass; + $features->groupings = true; + $this->standard_coursemodule_elements($features); + } else { + // Adding the "summary" field. + $mform->addElement('editor', 'summary_editor', get_string('summary', 'oublog'), null, + array('maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes' => $CFG->maxbytes)); + $mform->setType('summary', PARAM_RAW); + $mform->addElement('hidden', 'instance'); + $mform->setType('instance', PARAM_INT); + } + + // Add standard buttons, common to all modules. + $this->add_action_buttons(); + + } + + public function add_completion_rules() { + $mform =& $this->_form; + + $group=array(); + $group[] =& $mform->createElement('checkbox', 'completionpostsenabled', ' ', get_string('completionposts', 'oublog')); + $group[] =& $mform->createElement('text', 'completionposts', ' ', array('size'=>3)); + $mform->setType('completionposts', PARAM_INT); + $mform->addGroup($group, 'completionpostsgroup', get_string('completionpostsgroup', 'oublog'), array(' '), false); + $mform->addHelpButton('completionpostsgroup', 'completionpostsgroup', 'oublog'); + $mform->disabledIf('completionposts', 'completionpostsenabled', 'notchecked'); + + $group=array(); + $group[] =& $mform->createElement('checkbox', 'completioncommentsenabled', ' ', get_string('completioncomments', 'oublog')); + $group[] =& $mform->createElement('text', 'completioncomments', ' ', array('size'=>3)); + $mform->setType('completioncomments', PARAM_INT); + $mform->addGroup($group, 'completioncommentsgroup', get_string('completioncommentsgroup', 'oublog'), array(' '), false); + $mform->addHelpButton('completioncommentsgroup', 'completioncommentsgroup', 'oublog'); + $mform->disabledIf('completioncomments', 'completioncommentsenabled', 'notchecked'); + + // Restriction for grade completion + $mform->disabledIf('completionusegrade', 'grade', 'eq', 0); + + return array('completionpostsgroup', 'completioncommentsgroup'); + } + + public function completion_rule_enabled($data) { + return ((!empty($data['completionpostsenabled']) && $data['completionposts']!=0)) || + ((!empty($data['completioncommentsenabled']) && $data['completioncomments']!=0)); + } + + public function get_data() { + $data=parent::get_data(); + if (!$data) { + return false; + } + // Turn off completion settings if the checkboxes aren't ticked + if (!empty($data->completionunlocked)) { + $autocompletion = !empty($data->completion) && $data->completion == COMPLETION_TRACKING_AUTOMATIC; + if (empty($data->completionpostsenabled) || !$autocompletion) { + $data->completionposts = 0; + } + if (empty($data->completioncommentsenabled) || !$autocompletion) { + $data->completioncomments = 0; + } + } + // If maxvisibility is disabled by individual mode, ensure it's limited to course. + if (isset($data->individual) && ($data->individual == OUBLOG_SEPARATE_INDIVIDUAL_BLOGS + || $data->individual == OUBLOG_VISIBLE_INDIVIDUAL_BLOGS)) { + $data->maxvisibility = OUBLOG_VISIBILITY_COURSEUSER; + } + // Set the reportingemail to null if empty so that we have consistency. + if (empty($data->reportingemail)) { + $data->reportingemail = null; + } + // Set statblockon to null if empty so that we have consistency. + if (empty($data->statblockon)) { + $data->statblockon = 0; + } + if (empty($data->displayname)) { + $data->displayname = null; + } + if (empty($data->allowimport)) { + $data->allowimport = 0; + } + if (empty($data->introonpost)) { + $data->introonpost = 0; + } + if (!empty($data->tagslist)) { + $data->tagslist = trim($data->tagslist); + } + if (empty($data->restricttags)) { + $data->restricttags = 0; + } + if (empty($data->postfrom)) { + $data->postfrom = 0; + } + if (empty($data->postuntil)) { + $data->postuntil = 0; + } + if (empty($data->commentfrom)) { + $data->commentfrom = 0; + } + if (empty($data->commentuntil)) { + $data->commentuntil = 0; + } + if (isset($data->grading) && $data->grading == OUBLOG_NO_GRADING) { + // Unset grade if grading turned off. + $data->grade = 0; + } + return $data; + } + + public function data_preprocessing(&$default_values) { + // Set up the completion checkboxes which aren't part of standard data. + // We also make the default value (if you turn on the checkbox) for those + // numbers to be 1, this will not apply unless checkbox is ticked. + $default_values['completionpostsenabled']= + !empty($default_values['completionposts']) ? 1 : 0; + if (empty($default_values['completionposts'])) { + $default_values['completionposts']=1; + } + $default_values['completioncommentsenabled']= + !empty($default_values['completioncomments']) ? 1 : 0; + if (empty($default_values['completioncomments'])) { + $default_values['completioncomments']=1; + } + } + + public function validation($data, $files) { + global $DB; + $errors = parent::validation($data, $files); + if (!empty($data['groupmode']) && isset($data['allowcomments']) && + $data['allowcomments'] == OUBLOG_COMMENTS_ALLOWPUBLIC) { + $errors['allowcomments'] = get_string('error_grouppubliccomments', 'oublog'); + } + if (!empty($data['reportingemail'])) { + $emails = explode(',', trim($data['reportingemail'])); + foreach ($emails as $email) { + if (!validate_email($email)) { + $errors['reportingemail'] = get_string('invalidemail', 'forumng'); + } + } + } + if (!empty($data['allowimport']) && $data['individual'] == OUBLOG_NO_INDIVIDUAL_BLOGS) { + // Can only import on individual or global blogs. + if (!empty($data['instance'])) { + if (!$DB->get_field('oublog', 'global', array('id' => $data['instance']))) { + $errors['allowimport'] = get_string('allowimport_invalid', 'oublog'); + } + } else { + $errors['allowimport'] = get_string('allowimport_invalid', 'oublog'); + } + } + // If form is on blog edit page rather than the blog options edit instance page. + if (isset($data['grading'])) { + if (($data['grading'] == OUBLOG_TEACHER_GRADING && empty($data['grade'])) || + ($data['grading'] == OUBLOG_USE_RATING && empty($data['assessed']))) { + $errors['grading'] = get_string('grading_invalid', 'oublog'); + } + } + if (isset($data['restricttags']) && empty($data['tagslist']) + && ($data['restricttags'] == 1 || $data['restricttags'] == 3)) { + // When forcing use of pre-defined tags must define some. + $errors['tagslist'] = get_string('required'); + } + if (!empty($data['idsharedblog'])) { + $masterblog = oublog_get_master($data['idsharedblog'], false); + + // Cannot get master if it doesn't have ID number or has more than 1. + if (empty($masterblog)) { + $errors['idsharedblog'] = get_string('sharedblog_invalid', 'oublog'); + } + if (count($masterblog) > 1) { + $errors['idsharedblog'] = get_string('sharedblog_invalid_morethan1', 'oublog'); + } + // Cannot be child if it is master. + if (!empty($data['cmidnumber'])) { + $validatemasterblog = oublog_get_children($data['cmidnumber']); + if (!empty($validatemasterblog)) { + $errors['idsharedblog'] = get_string('sharedblog_mastered', 'oublog'); + } + } + + $masterblog = reset($masterblog); + // Cannot be master blog if it already child of the other blog. + if (!empty($masterblog->idsharedblog)) { + $errors['idsharedblog'] = get_string('sharedblog_existed', 'oublog'); + } + } + return $errors; + } +} diff --git a/participation_table.php b/participation_table.php index bd3b598..cf8ab0c 100644 --- a/participation_table.php +++ b/participation_table.php @@ -41,7 +41,7 @@ class oublog_participation_table extends flexible_table { public $extraheaders; private $hasgrades; - public function __construct($cm, $course, $oublog, $groupid = 0, + public function __construct($cm, $course, $oublog, $groupid, $groupname, $hasgrades) { $this->cm = $cm; diff --git a/pix/tick.svg b/pix/tick.svg index fd2a391..631ecc9 100644 --- a/pix/tick.svg +++ b/pix/tick.svg @@ -1,3 +1,3 @@ - + ]> \ No newline at end of file diff --git a/pluginfile.php b/pluginfile.php index 3543e90..6c4a531 100644 --- a/pluginfile.php +++ b/pluginfile.php @@ -31,7 +31,7 @@ // Relative path must start with '/'. if (!$relativepath) { print_error('invalidargorconf'); -} else if ($relativepath{0} != '/') { +} else if ($relativepath[0] != '/') { print_error('pathdoesnotstartslash'); } diff --git a/tests/locallib_test.php b/tests/locallib_test.php index 79eeb8d..17d3d18 100644 --- a/tests/locallib_test.php +++ b/tests/locallib_test.php @@ -31,6 +31,7 @@ global $CFG; require_once($CFG->dirroot . '/mod/oublog/tests/oublog_test_lib.php'); require_once($CFG->dirroot . '/mod/oublog/locallib.php'); +require_once($CFG->dirroot . '/mod/oublog/lib.php'); class oublog_locallib_test extends oublog_test_lib { @@ -63,6 +64,15 @@ class oublog_locallib_test extends oublog_test_lib { * Deleting blog (and checking no data left behind) */ + /** + * Create temporary test tables and entries in the database for these tests. + * These tests have to work on a brand new site. + */ + public function setUp(): void { + $this->resetAfterTest(); + $this->setAdminUser(); + } + public function test_oublog_add_post() { global $SITE, $USER; $this->resetAfterTest(true); @@ -1465,6 +1475,7 @@ public function test_oublog_tags() { // Change the predefined tags on the blog. $oublog->tagslist = 'blogtag01, blogtag02'; $oublog->instance = $oublog->id; + $oublog->coursemodule = $cm->id; oublog_update_instance($oublog); // Check that the changed tags are inserted to the oublog_tags table. $blogtags = oublog_clarify_tags($oublog->tagslist); @@ -1725,4 +1736,212 @@ public function test_can_view_post() { $this->assertEquals('normal:tttt,vi:tttt,si:tttt,visg:tftf,visgg:tfff,sisg:tftf,sisgg:tfff,vg:tttt,sg:tftf,sgg:tff,', $results[$staffvigroups->id]); } + + /** + * Test core_calendar provides the event for oublog. + */ + public function test_oublog_core_calendar_provide_event_action() { + // Create the activity. + $course = $this->getDataGenerator()->create_course(); + $oublog = $this->getDataGenerator()->create_module('oublog', ['course' => $course->id]); + + // Create a calendar event. + $event = $this->create_action_event($course->id, $oublog->id, + \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED); + + // Create an action factory. + $factory = new \core_calendar\action_factory(); + + // Decorate action event. + $actionevent = mod_oublog_core_calendar_provide_event_action($event, $factory); + + // Confirm the event was decorated. + $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent); + $this->assertEquals(get_string('view'), $actionevent->get_name()); + $this->assertInstanceOf('moodle_url', $actionevent->get_url()); + $this->assertEquals(1, $actionevent->get_item_count()); + $this->assertTrue($actionevent->is_actionable()); + } + + /** + * Test core_calendar hidden when oublog hides. + */ + public function test_oublog_core_calendar_provide_event_action_in_hidden_section() { + // Create the activity. + $course = $this->getDataGenerator()->create_course(); + $oublog = $this->getDataGenerator()->create_module('oublog', ['course' => $course->id]); + + // Enrol a student in the course. + $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); + + // Create a calendar event. + $event = $this->create_action_event($course->id, $oublog->id, + \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED); + + // Set sections 0 as hidden. + set_section_visible($course->id, 0, 0); + + // Now, log out. + $this->setUser(); + + // Create an action factory. + $factory = new \core_calendar\action_factory(); + + // Decorate action event for the student. + $actionevent = mod_oublog_core_calendar_provide_event_action($event, $factory, $student->id); + + // Confirm the event is not shown at all. + $this->assertNull($actionevent); + } + + /** + * Test core_calendar provides the events for user. + */ + public function test_oublog_core_calendar_provide_event_action_for_user() { + // Create the activity. + $course = $this->getDataGenerator()->create_course(); + $oublog = $this->getDataGenerator()->create_module('oublog', ['course' => $course->id]); + + // Enrol a student in the course. + $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); + + // Create a calendar event. + $event = $this->create_action_event($course->id, $oublog->id, + \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED); + + // Now, log out. + $this->setUser($student); + + // Create an action factory. + $factory = new \core_calendar\action_factory(); + + // Decorate action event for the student. + $actionevent = mod_oublog_core_calendar_provide_event_action($event, $factory, $student->id); + + // Confirm the event was decorated. + $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent); + $this->assertEquals(get_string('view'), $actionevent->get_name()); + $this->assertInstanceOf('moodle_url', $actionevent->get_url()); + $this->assertEquals(1, $actionevent->get_item_count()); + $this->assertTrue($actionevent->is_actionable()); + } + + /** + * Test core_calendar provides the events for non-user. + */ + public function test_oublog_core_calendar_provide_event_action_as_non_user() { + global $CFG; + + // Create the activity. + $course = $this->getDataGenerator()->create_course(); + $oublog = $this->getDataGenerator()->create_module('oublog', ['course' => $course->id]); + + // Create a calendar event. + $event = $this->create_action_event($course->id, $oublog->id, + \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED); + + // Log out the user and set force login to true. + \core\session\manager::init_empty_session(); + $CFG->forcelogin = true; + + // Create an action factory. + $factory = new \core_calendar\action_factory(); + + // Decorate action event. + $actionevent = mod_oublog_core_calendar_provide_event_action($event, $factory); + + // Ensure result was null. + $this->assertNull($actionevent); + } + + /** + * Test core_calendar provides the action events completed. + */ + public function test_oublog_core_calendar_provide_event_action_already_completed() { + global $CFG; + $CFG->enablecompletion = 1; + + // Create the activity. + $course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]); + $oublog = $this->getDataGenerator()->create_module('oublog', ['course' => $course->id], + ['completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS]); + + // Get some additional data. + $cm = get_coursemodule_from_instance('oublog', $oublog->id); + + // Create a calendar event. + $event = $this->create_action_event($course->id, $oublog->id, + \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED); + + // Mark the activity as completed. + $completion = new \completion_info($course); + $completion->set_module_viewed($cm); + + // Create an action factory. + $factory = new \core_calendar\action_factory(); + + // Decorate action event. + $actionevent = mod_oublog_core_calendar_provide_event_action($event, $factory); + + // Ensure result was null. + $this->assertNull($actionevent); + } + + /** + * Test core_calendar provides the action events completed for user. + */ + public function test_oublog_core_calendar_provide_event_action_already_completed_for_user() { + global $CFG; + + $CFG->enablecompletion = 1; + + // Create the activity. + $course = $this->getDataGenerator()->create_course(['enablecompletion' => 1]); + $oublog = $this->getDataGenerator()->create_module('oublog', ['course' => $course->id], + ['completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS]); + + // Enrol a student in the course. + $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); + + // Get some additional data. + $cm = get_coursemodule_from_instance('oublog', $oublog->id); + + // Create a calendar event. + $event = $this->create_action_event($course->id, $oublog->id, + \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED); + + // Mark the activity as completed for the student. + $completion = new \completion_info($course); + $completion->set_module_viewed($cm, $student->id); + + // Create an action factory. + $factory = new \core_calendar\action_factory(); + + // Decorate action event for the student. + $actionevent = mod_oublog_core_calendar_provide_event_action($event, $factory, $student->id); + + // Ensure result was null. + $this->assertNull($actionevent); + } + + /** + * Creates an action event. + * + * @param int $courseid The course id. + * @param int $instanceid The instance id. + * @param string $eventtype The event type. + * @return bool|calendar_event + */ + private function create_action_event($courseid, $instanceid, $eventtype) { + $event = new \stdClass(); + $event->name = 'Calendar event'; + $event->modulename = 'oublog'; + $event->courseid = $courseid; + $event->instance = $instanceid; + $event->type = CALENDAR_EVENT_TYPE_ACTION; + $event->eventtype = $eventtype; + $event->timestart = time(); + + return \calendar_event::create($event); + } } diff --git a/tests/oublog_search_test.php b/tests/oublog_search_test.php index 33ed4a0..383d95d 100644 --- a/tests/oublog_search_test.php +++ b/tests/oublog_search_test.php @@ -149,7 +149,7 @@ public function test_post_search_index() { $this->assertCount(0, $out->get_files()); $searcharea->attach_files($out); $files = $out->get_files(); - $this->assertCount(4, $files); + $this->assertCount(2, $files); foreach ($files as $file) { if ($file->is_directory()) { continue;