diff --git a/duration_form.php b/duration_form.php index b09ac83..e0083e5 100644 --- a/duration_form.php +++ b/duration_form.php @@ -29,7 +29,7 @@ class mod_attforblock_duration_form extends moodleform { $mform->addElement('hidden', 'ids', $ids); $mform->addElement('hidden', 'id', $cm->id); - $mform->addElement('hidden', 'action', 'changeduration'); + $mform->addElement('hidden', 'action', att_sessions_page_params::ACTION_CHANGE_DURATION); $mform->setDefaults(array('durtime' => array('hours'=>0, 'minutes'=>0))); diff --git a/lang/en/attforblock.php b/lang/en/attforblock.php index a42d284..f94e2bc 100644 --- a/lang/en/attforblock.php +++ b/lang/en/attforblock.php @@ -110,6 +110,7 @@ $string['noofdayslate'] = 'No of days late'; $string['noofdayspresent'] = 'No of days present'; $string['nosessiondayselected'] = 'No Session day selected'; $string['nosessionexists'] = 'No Session exists for this course'; +$string['nosessionsselected'] = 'No sessions selected'; $string['notfound'] = 'Attendance activity not found in this course!'; $string['olddate'] = 'Old date'; $string['period'] = 'Frequency'; @@ -121,6 +122,7 @@ $string['resetdescription'] = 'Remember that deleting attendance data will erase $string['resetstatuses'] = 'Reset statuses to default'; $string['restoredefaults'] = 'Restore defaults'; $string['save'] = 'Save attendance'; +$string['session'] = 'Session'; $string['session_help'] = 'Session'; $string['sessionadded'] = 'Session successfully added'; $string['sessionalreadyexists'] = 'Session already exists for this date'; diff --git a/lang/ru/attforblock.php b/lang/ru/attforblock.php index 3215395..61f0c09 100644 --- a/lang/ru/attforblock.php +++ b/lang/ru/attforblock.php @@ -96,6 +96,7 @@ $string['noofdayslate'] = 'Опозданий'; $string['noofdayspresent'] = 'Вы присутствовали'; $string['nosessiondayselected'] = 'Не выбран день занятия'; $string['nosessionexists'] = 'В данном курсе отсутствуют занятия. Сначала добавьте их'; +$string['nosessionsselected'] = 'Не выбрано ни одного занятия'; $string['notfound'] = 'Элемент курса - \"Посещаемость\" не найден в данном курсе!'; $string['olddate'] = 'Старая дата'; $string['period'] = 'Периодичность'; diff --git a/locallib.php b/locallib.php index 2f5721b..7aad9a1 100644 --- a/locallib.php +++ b/locallib.php @@ -329,9 +329,14 @@ class attforblock { private $currentgroup; - private $sessioninfo; private $statuses; + + // Cache + + // array by sessionid + private $sessioninfo = array(); + // arrays by userid private $usertakensesscount = array(); private $userstatusesstat = array(); @@ -480,7 +485,13 @@ class attforblock { 'cgroup' => $this->get_current_group()); $sessions = $DB->get_records_select('attendance_sessions', $where, $params, 'sessdate asc'); foreach ($sessions as $sess) { - $sess->description = file_rewrite_pluginfile_urls($sess->description, 'pluginfile.php', $this->context->id, 'mod_attforblock', 'session', $sess->id); + if (empty($sess->description)) { + $sess->description = get_string('nodescription', 'attforblock'); + } + else { + $sess->description = file_rewrite_pluginfile_urls($sess->description, + 'pluginfile.php', $this->context->id, 'mod_attforblock', 'session', $sess->id); + } } return $sessions; @@ -814,12 +825,36 @@ class attforblock { public function get_session_info($sessionid) { global $DB; - if (!isset($this->sessioninfo)) - $this->sessioninfo = $DB->get_record('attendance_sessions', array('id' => $sessionid)); - $this->sessioninfo->description = file_rewrite_pluginfile_urls($this->sessioninfo->description, - 'pluginfile.php', $this->context->id, 'mod_attforblock', 'session', $this->sessioninfo->id); + if (!array_key_exists($sessionid, $this->sessioninfo)) + $this->sessioninfo[$sessionid] = $DB->get_record('attendance_sessions', array('id' => $sessionid)); + if (empty($this->sessioninfo[$sessionid]->description)) { + $this->sessioninfo[$sessionid]->description = get_string('nodescription', 'attforblock'); + } + else { + $this->sessioninfo[$sessionid]->description = file_rewrite_pluginfile_urls($this->sessioninfo[$sessionid]->description, + 'pluginfile.php', $this->context->id, 'mod_attforblock', 'session', $this->sessioninfo[$sessionid]->id); + } + + return $this->sessioninfo[$sessionid]; + } + + public function get_sessions_info($sessionids) { + global $DB; + + list($sql, $params) = $DB->get_in_or_equal($sessionids); + $sessions = $DB->get_records_select('attendance_sessions', "id $sql", $params, 'sessdate asc'); - return $this->sessioninfo; + foreach ($sessions as $sess) { + if (empty($sess->description)) { + $sess->description = get_string('nodescription', 'attforblock'); + } + else { + $sess->description = file_rewrite_pluginfile_urls($sess->description, + 'pluginfile.php', $this->context->id, 'mod_attforblock', 'session', $sess->id); + } + } + + return $sessions; } public function get_session_log($sessionid) { @@ -890,7 +925,7 @@ class attforblock { foreach ($userids as $userid) { $grades[$userid]->userid = $userid; - $grades[$userid]->rawgrade = $this->get_user_grade($userid) / $this->get_user_max_grade($userid) * 100; + $grades[$userid]->rawgrade = calc_user_grade_percent($this->get_user_grade($userid), $this->get_user_max_grade($userid)); } return grade_update('mod/attforblock', $this->course->id, 'mod', 'attforblock', @@ -922,11 +957,40 @@ class attforblock { 'edate' => $this->pageparams->enddate); $sessions = $DB->get_records_sql($sql, $params); foreach ($sessions as $sess) { - $sess->description = file_rewrite_pluginfile_urls($sess->description, 'pluginfile.php', $this->context->id, 'mod_attforblock', 'session', $sess->id); + if (empty($sess->description)) { + $sess->description = get_string('nodescription', 'attforblock'); + } + else { + $sess->description = file_rewrite_pluginfile_urls($sess->description, + 'pluginfile.php', $this->context->id, 'mod_attforblock', 'session', $sess->id); + } } return $sessions; } + + public function delete_sessions($sessionsids) { + global $DB; + + list($sql, $params) = $DB->get_in_or_equal($sessionsids); + $DB->delete_records_select('attendance_log', "sessionid $sql", $params); + $DB->delete_records_list('attendance_sessions', 'id', $sessionsids); + + // TODO: log + } + + public function change_sessions_duration($sessionsids, $duration) { + global $DB; + + $now = time(); + $sessions = $DB->get_records_list('attendance_sessions', 'id', $sessionsids); + foreach ($sessions as $sess) { + $sess->duration = $duration; + $sess->timemodified = $now; + $DB->update_record('attendance_sessions', $sess); + } + // TODO: log + } } @@ -1013,19 +1077,26 @@ function get_user_courses_attendances($userid) { return $DB->get_records_sql($sql, $params); } +function calc_user_grade_percent($grade, $maxgrade) { + if ($maxgrade == 0) + return 0; + else + return $grade / $maxgrade * 100; +} + function update_all_users_grades($attid, $course, $context) { global $COURSE; $grades = array(); - $userids = get_enrolled_users($context, 'mod/attforblock:canbelisted', 0, 'u.id'); + $userids = array_keys(get_enrolled_users($context, 'mod/attforblock:canbelisted', 0, 'u.id')); $statuses = get_statuses($attid); foreach ($userids as $userid) { $grades[$userid]->userid = $userid; $userstatusesstat = get_user_statuses_stat($attid, $course->startdate, $userid); $usertakensesscount = get_user_taken_sessions_count($attid, $course->startdate, $userid); - $grades[$userid]->rawgrade = get_user_grade($userstatusesstat, $statuses) / get_user_max_grade($usertakensesscount, $statuses) * 100; + $grades[$userid]->rawgrade = calc_user_grade_percent(get_user_grade($userstatusesstat, $statuses), get_user_max_grade($usertakensesscount, $statuses)); } return grade_update('mod/attforblock', $course->id, 'mod', 'attforblock', diff --git a/renderer.php b/renderer.php index db5128c..2c9532a 100644 --- a/renderer.php +++ b/renderer.php @@ -185,9 +185,9 @@ class mod_attforblock_renderer extends plugin_renderer_base { $table->data[$sess->id][] = $sess->groupid ? $sessdata->groups[$sess->groupid]->name : get_string('commonsession', 'attforblock'); $table->data[$sess->id][] = $dta['date']; $table->data[$sess->id][] = $dta['time']; - $table->data[$sess->id][] = empty($sess->description) ? get_string('nodescription', 'attforblock') : $sess->description; + $table->data[$sess->id][] = $sess->description; $table->data[$sess->id][] = $dta['actions']; - $table->data[$sess->id][] = html_writer::checkbox('sessid', $sess->id, false); + $table->data[$sess->id][] = html_writer::checkbox('sessid[]', $sess->id, false); } return html_writer::table($table); @@ -240,8 +240,9 @@ class mod_attforblock_renderer extends plugin_renderer_base { get_string('hiddensessions', 'attforblock').': '.$sessdata->hiddensessionscount); if ($sessdata->perm->can_manage()) { - $options = array('deleteselected' => get_string('delete'), - 'changeduration' => get_string('changeduration', 'attforblock')); + $options = array( + att_sessions_page_params::ACTION_DELETE_SELECTED => get_string('delete'), + att_sessions_page_params::ACTION_CHANGE_DURATION => get_string('changeduration', 'attforblock')); $controls = html_writer::select($options, 'action'); $attributes = array( 'type' => 'submit', @@ -292,7 +293,7 @@ class mod_attforblock_renderer extends plugin_renderer_base { $sessinfo = $date.' '.$time; $sessinfo .= html_writer::empty_tag('br'); $sessinfo .= html_writer::empty_tag('br'); - $sessinfo .= empty($sess->description) ? get_string('nodescription', 'attforblock') : $sess->description; + $sessinfo .= $sess->description; return $sessinfo; } @@ -608,7 +609,7 @@ class mod_attforblock_renderer extends plugin_renderer_base { $row->cells[] = $i; $row->cells[] = userdate($sess->sessdate, get_string('strftimedmyw', 'attforblock')); $row->cells[] = $this->construct_time($sess->sessdate, $sess->duration); - $row->cells[] = empty($sess->description) ? get_string('nodescription', 'attforblock') : $sess->description; + $row->cells[] = $sess->description; if (isset($sess->statusid)) { $row->cells[] = $userdata->statuses[$sess->statusid]->description; $row->cells[] = $sess->remarks; diff --git a/sessions.php b/sessions.php index 54314be..570d62f 100644 --- a/sessions.php +++ b/sessions.php @@ -44,7 +44,6 @@ switch ($att->pageparams->action) { redirect($url, get_string('sessionadded','attforblock')); } } - break; case att_sessions_page_params::ACTION_UPDATE: $sessionid = required_param('sessionid', PARAM_INT); @@ -63,6 +62,92 @@ switch ($att->pageparams->action) { redirect($att->url_manage(), get_string('sessionupdated','attforblock')); } break; + case att_sessions_page_params::ACTION_DELETE: + $sessionid = required_param('sessionid', PARAM_INT); + $confirm = optional_param('confirm', NULL, PARAM_INT); + + if (isset($confirm)) { + $att->delete_sessions(array($sessionid)); + update_all_users_grades($att->id, $att->course, $att->context); + redirect($att->url_manage(), get_string('sessiondeleted','attforblock')); + } + + $sessinfo = $att->get_session_info($sessionid); + + $message = get_string('deletecheckfull', '', get_string('session', 'attforblock')); + $message .= str_repeat(html_writer::empty_tag('br'), 2); + $message .= userdate($sessinfo->sessdate, get_string('strftimedmyhm', 'attforblock')); + $message .= html_writer::empty_tag('br'); + $message .= $sessinfo->description; + + $params = array('action' => $att->pageparams->action, 'sessionid' => $sessionid, 'confirm' => 1); + + echo $OUTPUT->header(); + echo $OUTPUT->heading(get_string('attendanceforthecourse','attforblock').' :: ' .$course->fullname); + echo $OUTPUT->confirm($message, $att->url_sessions($params), $att->url_manage()); + echo $OUTPUT->footer(); + exit; + case att_sessions_page_params::ACTION_DELETE_SELECTED: + $confirm = optional_param('confirm', NULL, PARAM_INT); + + if (isset($confirm)) { + $sessionsids = required_param('sessionsids', PARAM_ALPHANUMEXT); + $sessionsids = explode('_', $sessionsids); + + $att->delete_sessions($sessionsids); + update_all_users_grades($att->id, $att->course, $att->context); + redirect($att->url_manage(), get_string('sessiondeleted','attforblock')); + } + + $fromform = data_submitted(); + // nothing selected + if (!isset($fromform->sessid)) + print_error ('nosessionsselected','attforblock', $att->url_manage()); + + $sessionsinfo = $att->get_sessions_info($fromform->sessid); + + $message = get_string('deletecheckfull', '', get_string('session', 'attforblock')); + $message .= html_writer::empty_tag('br'); + foreach ($sessionsinfo as $sessinfo) { + $message .= html_writer::empty_tag('br'); + $message .= userdate($sessinfo->sessdate, get_string('strftimedmyhm', 'attforblock')); + $message .= html_writer::empty_tag('br'); + $message .= $sessinfo->description; + } + + $sessionsids = implode('_', $fromform->sessid); + $params = array('action' => $att->pageparams->action, 'sessionsids' => $sessionsids, 'confirm' => 1); + + echo $OUTPUT->header(); + echo $OUTPUT->heading(get_string('attendanceforthecourse','attforblock').' :: ' .$course->fullname); + echo $OUTPUT->confirm($message, $att->url_sessions($params), $att->url_manage()); + echo $OUTPUT->footer(); + exit; + case att_sessions_page_params::ACTION_CHANGE_DURATION: + $fromform = data_submitted(); + $slist = isset($fromform->sessid) ? implode('_', $fromform->sessid) : ''; + + $url = $att->url_sessions(array('action' => att_sessions_page_params::ACTION_CHANGE_DURATION)); + $formparams['ids'] = $slist; + $mform = new mod_attforblock_duration_form($url, $formparams); + + if ($mform->is_cancelled()) { + redirect($att->url_manage()); + } + + if ($mform->is_submitted()) { + $formdata = $mform->get_data(); + + $sessionsids = explode('_', $fromform->ids); + $duration = $formdata->durtime['hours']*HOURSECS + $formdata->durtime['minutes']*MINSECS; + $att->change_sessions_duration($sessionsids, $duration); + redirect($att->url_manage(), get_string('sessionupdated','attforblock')); + } + + if ($slist === '') + print_error ('nosessionsselected','attforblock', $att->url_manage()); + + break; } $output = $PAGE->get_renderer('mod_attforblock');